use of com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedException in project java-spanner-jdbc by googleapis.
the class JdbcAbortedTransactionTest method testTransactionalBatchUpdateAborted.
@Test
public void testTransactionalBatchUpdateAborted() throws SQLException {
try (java.sql.Connection connection = createConnection()) {
connection.setAutoCommit(false);
mockSpanner.abortNextStatement();
try (java.sql.Statement statement = connection.createStatement()) {
statement.addBatch(UPDATE_STATEMENT.getSql());
statement.addBatch(UPDATE_STATEMENT.getSql());
int[] updateCounts = statement.executeBatch();
if (!retryAbortsInternally) {
fail("missing expected exception");
}
assertThat(updateCounts).asList().containsExactly(UPDATE_COUNT, UPDATE_COUNT);
assertThat(getRetryCount(connection)).isEqualTo(1);
} catch (JdbcAbortedException e) {
assertThat(retryAbortsInternally).isFalse();
}
}
}
use of com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedException in project java-docs-samples by GoogleCloudPlatform.
the class TransactionWithRetryLoopExample method transactionWithRetryLoop.
static void transactionWithRetryLoop(String projectId, String instanceId, String databaseId) throws SQLException {
// Create a connection that has automatic retry for aborted transactions disabled.
String connectionUrl = String.format("jdbc:cloudspanner:/projects/%s/instances/%s/databases/%s" + ";retryAbortsInternally=false", projectId, instanceId, databaseId);
long singerId = 31;
long albumId = 11;
try (Connection connection = DriverManager.getConnection(connectionUrl)) {
while (true) {
try {
CloudSpannerJdbcConnection spannerConnection = connection.unwrap(CloudSpannerJdbcConnection.class);
spannerConnection.setAutoCommit(false);
Mutation mutationSingers = Mutation.newInsertBuilder("Singers").set("SingerId").to(singerId).set("FirstName").to("Breanna").set("LastName").to("Fountain").set("Revenues").to(new BigDecimal("29809.93")).build();
Mutation mutationAlbums = Mutation.newInsertBuilder("Albums").set("SingerId").to(singerId).set("AlbumId").to(albumId).set("AlbumTitle").to("No discounts").set("MarketingBudget").to(1000).build();
spannerConnection.bufferedWrite(Arrays.asList(mutationSingers, mutationAlbums));
spannerConnection.commit();
System.out.printf("Transaction committed at [%s]%n", spannerConnection.getCommitTimestamp().toString());
break;
} catch (JdbcAbortedException e) {
// Rollback the current transaction to initiate a new transaction on the next statement.
connection.rollback();
// Transaction aborted, retry.
System.out.println("Transaction aborted, starting retry");
}
}
}
}
use of com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedException in project java-spanner-jdbc by googleapis.
the class JdbcAbortedTransactionTest method testTransactionalUpdateWithErrorOnOriginalAndNotOnRetry.
@Test
public void testTransactionalUpdateWithErrorOnOriginalAndNotOnRetry() throws SQLException {
final String sql = "UPDATE SOMETHING SET OTHER=1";
mockSpanner.putStatementResult(StatementResult.exception(Statement.of(sql), Status.INVALID_ARGUMENT.withDescription("test").asRuntimeException()));
try (java.sql.Connection connection = createConnection()) {
connection.setAutoCommit(false);
try (ResultSet rs = connection.createStatement().executeQuery(SELECT1.getSql())) {
while (rs.next()) {
assertThat(rs.getLong(1)).isEqualTo(1L);
}
}
try {
connection.createStatement().executeUpdate(sql);
fail("missing 'test' exception");
} catch (SQLException e) {
// ignore
}
// Set the update statement to return a result next time (i.e. during retry).
mockSpanner.putStatementResult(StatementResult.update(Statement.of(sql), 1L));
mockSpanner.abortNextStatement();
connection.commit();
fail("missing expected aborted exception");
} catch (JdbcAbortedDueToConcurrentModificationException e) {
assertThat(retryAbortsInternally).isTrue();
assertThat(e.getDatabaseErrorDuringRetry()).isNull();
} catch (JdbcAbortedException e) {
assertThat(retryAbortsInternally).isFalse();
}
}
use of com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedException in project java-spanner-jdbc by googleapis.
the class JdbcAbortedTransactionTest method testTransactionalUpdateAborted.
@Test
public void testTransactionalUpdateAborted() throws SQLException {
// off.
try (java.sql.Connection connection = createConnection()) {
connection.setAutoCommit(false);
mockSpanner.abortNextStatement();
int updateCount = connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql());
if (!retryAbortsInternally) {
fail("missing expected exception");
}
assertThat(updateCount).isEqualTo(UPDATE_COUNT);
assertThat(getRetryCount(connection)).isEqualTo(1);
} catch (JdbcAbortedException e) {
assertThat(retryAbortsInternally).isFalse();
}
}
use of com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedException in project java-spanner-jdbc by googleapis.
the class JdbcAbortedTransactionTest method testTransactionalUpdateWithErrorOnOriginalAndRetry.
@Test
public void testTransactionalUpdateWithErrorOnOriginalAndRetry() throws SQLException {
final String sql = "UPDATE SOMETHING SET OTHER=1";
mockSpanner.putStatementResult(StatementResult.exception(Statement.of(sql), Status.INVALID_ARGUMENT.withDescription("test").asRuntimeException()));
try (java.sql.Connection connection = createConnection()) {
connection.setAutoCommit(false);
try (ResultSet rs = connection.createStatement().executeQuery(SELECT1.getSql())) {
while (rs.next()) {
assertThat(rs.getLong(1)).isEqualTo(1L);
}
}
try {
connection.createStatement().executeUpdate(sql);
fail("missing 'test' exception");
} catch (SQLException e) {
// ignore
}
mockSpanner.abortNextStatement();
connection.commit();
if (!retryAbortsInternally) {
fail("missing expected exception");
}
} catch (JdbcAbortedException e) {
assertThat(retryAbortsInternally).isFalse();
}
}
Aggregations