Search in sources :

Example 1 with AbortInterceptor

use of com.google.cloud.spanner.connection.ITAbstractSpannerTest.AbortInterceptor in project java-spanner by googleapis.

the class AbortedTest method testCommitAborted.

@Test
public void testCommitAborted() {
    // transaction is the most recent transaction of that session.
    for (int i = 0; i < 2; i++) {
        mockSpanner.putStatementResult(StatementResult.query(SELECT_COUNT_STATEMENT, SELECT_COUNT_RESULTSET_BEFORE_INSERT));
        mockSpanner.putStatementResult(StatementResult.update(INSERT_STATEMENT, UPDATE_COUNT));
        AbortInterceptor interceptor = new AbortInterceptor(0);
        try (ITConnection connection = createConnection(interceptor, new CountTransactionRetryListener())) {
            // verify that the there is no test record
            try (ResultSet rs = connection.executeQuery(Statement.of("SELECT COUNT(*) AS C FROM TEST WHERE ID=1"))) {
                assertThat(rs.next(), is(true));
                assertThat(rs.getLong("C"), is(equalTo(0L)));
                assertThat(rs.next(), is(false));
            }
            // do an insert
            connection.executeUpdate(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (1, 'test aborted')"));
            // indicate that the next statement should abort
            interceptor.setProbability(1.0);
            interceptor.setOnlyInjectOnce(true);
            // do a commit that will first abort, and then on retry will succeed
            connection.commit();
            mockSpanner.putStatementResult(StatementResult.query(SELECT_COUNT_STATEMENT, SELECT_COUNT_RESULTSET_AFTER_INSERT));
            // verify that the insert succeeded
            try (ResultSet rs = connection.executeQuery(Statement.of("SELECT COUNT(*) AS C FROM TEST WHERE ID=1"))) {
                assertThat(rs.next(), is(true));
                assertThat(rs.getLong("C"), is(equalTo(1L)));
                assertThat(rs.next(), is(false));
            }
        }
    }
}
Also used : ITConnection(com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection) AbortInterceptor(com.google.cloud.spanner.connection.ITAbstractSpannerTest.AbortInterceptor) ResultSet(com.google.cloud.spanner.ResultSet) CountTransactionRetryListener(com.google.cloud.spanner.connection.it.ITTransactionRetryTest.CountTransactionRetryListener) Test(org.junit.Test)

Aggregations

ResultSet (com.google.cloud.spanner.ResultSet)1 AbortInterceptor (com.google.cloud.spanner.connection.ITAbstractSpannerTest.AbortInterceptor)1 ITConnection (com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection)1 CountTransactionRetryListener (com.google.cloud.spanner.connection.it.ITTransactionRetryTest.CountTransactionRetryListener)1 Test (org.junit.Test)1