Search in sources :

Example 1 with RandomResultSetGenerator

use of com.google.cloud.spanner.connection.RandomResultSetGenerator 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();
    }
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) JdbcAbortedDueToConcurrentModificationException(com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedDueToConcurrentModificationException) RandomResultSetGenerator(com.google.cloud.spanner.connection.RandomResultSetGenerator) JdbcAbortedException(com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedException) Test(org.junit.Test)

Aggregations

RandomResultSetGenerator (com.google.cloud.spanner.connection.RandomResultSetGenerator)1 JdbcAbortedDueToConcurrentModificationException (com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedDueToConcurrentModificationException)1 JdbcAbortedException (com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedException)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 Test (org.junit.Test)1