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);
}
}
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();
}
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);
}
}
}
Aggregations