Search in sources :

Example 6 with SpannerException

use of com.google.cloud.spanner.SpannerException in project spanner-jdbc by olavloite.

the class TransactionThread method stopTransaction.

private void stopTransaction(TransactionStopStatement statement) throws SQLException {
    if (status == TransactionStatus.FAIL || status == TransactionStatus.SUCCESS)
        return;
    while (status == TransactionStatus.NOT_STARTED) {
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new CloudSpannerSQLException(getFailedMessage(statement, e), Code.ABORTED, e);
        }
    }
    this.stopStatement = statement;
    stop = true;
    // Add a statement object in order to get the transaction thread to
    // proceed
    statements.add(Statement.of(statement.name()));
    synchronized (monitor) {
        while (!stopped || status == TransactionStatus.NOT_STARTED || status == TransactionStatus.RUNNING) {
            try {
                monitor.wait();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new CloudSpannerSQLException(getFailedMessage(statement, e), Code.ABORTED, e);
            }
        }
    }
    if (status == TransactionStatus.FAIL && exception != null) {
        Code code = Code.UNKNOWN;
        if (exception instanceof CloudSpannerSQLException)
            code = ((CloudSpannerSQLException) exception).getCode();
        if (exception instanceof SpannerException)
            code = Code.forNumber(((SpannerException) exception).getCode());
        throw new CloudSpannerSQLException(getFailedMessage(statement, exception), code, exception);
    }
}
Also used : SpannerException(com.google.cloud.spanner.SpannerException) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) Code(com.google.rpc.Code)

Example 7 with SpannerException

use of com.google.cloud.spanner.SpannerException in project google-cloud-java by GoogleCloudPlatform.

the class ITTransactionTest method userExceptionIsSpannerException.

@Test
public void userExceptionIsSpannerException() {
    final String key = uniqueKey();
    TransactionCallable<Void> callable = new TransactionCallable<Void>() {

        @Override
        public Void run(TransactionContext transaction) {
            transaction.buffer(Mutation.newInsertOrUpdateBuilder("T").set("K").to(key).build());
            throw newSpannerException(ErrorCode.OUT_OF_RANGE, "User failure");
        }
    };
    try {
        client.readWriteTransaction().run(callable);
        fail("Expected user exception");
    } catch (SpannerException e) {
        assertThat(e.getErrorCode()).isEqualTo(ErrorCode.OUT_OF_RANGE);
        assertThat(e.getMessage()).contains("User failure");
    }
    Struct row = client.singleUse(TimestampBound.strong()).readRow("T", Key.of(key), Arrays.asList("K"));
    assertThat(row).isNull();
}
Also used : TransactionCallable(com.google.cloud.spanner.TransactionRunner.TransactionCallable) TransactionContext(com.google.cloud.spanner.TransactionContext) SpannerExceptionFactory.newSpannerException(com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException) SpannerException(com.google.cloud.spanner.SpannerException) Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 8 with SpannerException

use of com.google.cloud.spanner.SpannerException in project spanner-jdbc by olavloite.

the class AbstractTablePartWorker method genericRun.

protected void genericRun() throws SQLException {
    String unquotedTableName = CloudSpannerDriver.unquoteIdentifier(getTable().getName());
    List<String> columnNamesList = getColumnNames();
    long batchSize = ConverterUtils.calculateActualBatchSize(columnNamesList.size(), connection, null, null, unquotedTableName);
    boolean isExtendedMode = isExtendedMode(batchSize);
    boolean wasAutocommit = connection.getAutoCommit();
    if (!isExtendedMode && wasAutocommit) {
        connection.setAutoCommit(false);
    }
    try (Connection destination = isExtendedMode ? connection.createCopyConnection() : null) {
        if (destination != null) {
            destination.setAutoCommit(false);
        }
        String sql = createSQL();
        try (PreparedStatement statement = destination == null ? connection.prepareStatement(sql) : destination.prepareStatement(sql)) {
            if (operation == DMLOperation.UPDATE) {
                // Set force update
                ((CloudSpannerPreparedStatement) statement).setForceUpdate(true);
            }
            CloudSpannerPreparedStatement selectStatement = connection.prepareStatement(select.toString());
            for (int i = 1; i <= parameters.getHighestIndex(); i++) {
                selectStatement.setObject(i, parameters.getParameter(i));
            }
            try (ResultSet rs = selectStatement.executeQuery()) {
                ResultSetMetaData metadata = rs.getMetaData();
                while (rs.next()) {
                    for (int index = 1; index <= metadata.getColumnCount(); index++) {
                        Object object = rs.getObject(index);
                        statement.setObject(index, object);
                    }
                    statement.executeUpdate();
                    recordCount++;
                    if (destination != null && recordCount % batchSize == 0)
                        destination.commit();
                }
            }
        }
        if (destination != null) {
            destination.commit();
        }
        if (wasAutocommit && !isExtendedMode) {
            connection.commit();
            connection.setAutoCommit(true);
        }
    } catch (SpannerException e) {
        throw new CloudSpannerSQLException(e);
    } catch (CloudSpannerSQLException e) {
        throw e;
    } catch (Exception e) {
        throw new CloudSpannerSQLException(e.getMessage(), Code.UNKNOWN, e);
    } finally {
        if (wasAutocommit && !isExtendedMode) {
            connection.rollback();
            connection.setAutoCommit(true);
        }
    }
}
Also used : CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) SpannerException(com.google.cloud.spanner.SpannerException) SQLException(java.sql.SQLException) ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) SpannerException(com.google.cloud.spanner.SpannerException)

Aggregations

SpannerException (com.google.cloud.spanner.SpannerException)8 IntegrationTest (com.google.cloud.spanner.IntegrationTest)5 Test (org.junit.Test)5 SpannerExceptionFactory.newSpannerException (com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException)3 Struct (com.google.cloud.spanner.Struct)3 TransactionContext (com.google.cloud.spanner.TransactionContext)3 AbortedException (com.google.cloud.spanner.AbortedException)2 SpannerMatchers.isSpannerException (com.google.cloud.spanner.SpannerMatchers.isSpannerException)2 TransactionCallable (com.google.cloud.spanner.TransactionRunner.TransactionCallable)2 Context (io.grpc.Context)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 CloudSpannerSQLException (nl.topicus.jdbc.exception.CloudSpannerSQLException)2 Database (com.google.cloud.spanner.Database)1 Code (com.google.rpc.Code)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1