use of com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedDueToConcurrentModificationException 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.JdbcAbortedDueToConcurrentModificationException in project java-spanner-jdbc by googleapis.
the class JdbcAbortedTransactionTest method testTransactionalUpdateWithConcurrentModificationsAborted.
@Test
public void testTransactionalUpdateWithConcurrentModificationsAborted() throws SQLException {
// original attempt.
try (java.sql.Connection connection = createConnection()) {
connection.setAutoCommit(false);
// Set a random answer.
mockSpanner.putStatementResult(StatementResult.query(SELECT_RANDOM, new RandomResultSetGenerator(25).generate()));
try (ResultSet rs = connection.createStatement().executeQuery(SELECT_RANDOM.getSql())) {
// noinspection StatementWithEmptyBody
while (rs.next()) {
}
}
// Set a new random answer that will be returned during the retry.
mockSpanner.putStatementResult(StatementResult.query(SELECT_RANDOM, new RandomResultSetGenerator(25).generate()));
// Abort all transactions (including the current one).
mockSpanner.abortNextStatement();
// This will abort and start an internal retry.
connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql());
fail("missing expected aborted exception");
} catch (JdbcAbortedDueToConcurrentModificationException e) {
assertThat(retryAbortsInternally).isTrue();
} catch (JdbcAbortedException e) {
assertThat(retryAbortsInternally).isFalse();
}
}
use of com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedDueToConcurrentModificationException in project java-spanner-jdbc by googleapis.
the class JdbcAbortedTransactionTest method testTransactionalUpdateWithErrorOnRetryAndNotOnOriginal.
@Test
public void testTransactionalUpdateWithErrorOnRetryAndNotOnOriginal() throws SQLException {
final String sql = "UPDATE SOMETHING SET OTHER=1";
try (java.sql.Connection connection = createConnection()) {
connection.setAutoCommit(false);
// Set a normal response to the update statement.
mockSpanner.putStatementResult(StatementResult.update(Statement.of(sql), 1L));
connection.createStatement().executeUpdate(sql);
// Set an error as response for the same update statement that will be used during the retry.
// This will cause the retry to fail.
mockSpanner.putStatementResult(StatementResult.exception(Statement.of(sql), Status.INVALID_ARGUMENT.withDescription("test").asRuntimeException()));
mockSpanner.abortNextStatement();
connection.commit();
fail("missing expected aborted exception");
} catch (JdbcAbortedDueToConcurrentModificationException e) {
assertThat(retryAbortsInternally).isTrue();
assertThat(e.getDatabaseErrorDuringRetry().getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
assertThat(e.getDatabaseErrorDuringRetry().getMessage()).endsWith("test");
} catch (JdbcAbortedException e) {
assertThat(retryAbortsInternally).isFalse();
}
}
Aggregations