Search in sources :

Example 21 with AbortedDueToConcurrentModificationException

use of com.google.cloud.spanner.AbortedDueToConcurrentModificationException in project java-spanner by googleapis.

the class ITTransactionRetryTest method testAbortWithConcurrentUpdate.

@Test
public void testAbortWithConcurrentUpdate() {
    assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
    AbortInterceptor interceptor = new AbortInterceptor(0);
    // first insert two test records
    try (ITConnection connection = createConnection()) {
        connection.executeUpdate(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (1, 'test 1')"));
        connection.executeUpdate(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (2, 'test 2')"));
        connection.commit();
    }
    // open a new connection and select the two test records
    try (ITConnection connection = createConnection(interceptor, new CountTransactionRetryListener())) {
        // select the test records and consume the entire result set
        try (ResultSet rs = connection.executeQuery(Statement.of("SELECT * FROM TEST ORDER BY ID"))) {
            while (rs.next()) {
            // do nothing
            }
        }
        // open a new connection and transaction and update one of the test records
        try (ITConnection connection2 = createConnection()) {
            connection2.executeUpdate(Statement.of("UPDATE TEST SET NAME='test updated' WHERE ID=2"));
            connection2.commit();
        }
        // now try to do an insert that will abort. The retry should now fail as there has been a
        // concurrent modification
        interceptor.setProbability(1.0);
        interceptor.setOnlyInjectOnce(true);
        boolean expectedException = false;
        try {
            connection.executeUpdate(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (3, 'test 3')"));
        } catch (AbortedDueToConcurrentModificationException e) {
            expectedException = true;
        }
        assertThat(expectedException, is(true));
        assertRetryStatistics(1, 1, 0);
    }
}
Also used : ResultSet(com.google.cloud.spanner.ResultSet) AbortedDueToConcurrentModificationException(com.google.cloud.spanner.AbortedDueToConcurrentModificationException) ParallelIntegrationTest(com.google.cloud.spanner.ParallelIntegrationTest) Test(org.junit.Test) ITAbstractSpannerTest(com.google.cloud.spanner.connection.ITAbstractSpannerTest)

Aggregations

AbortedDueToConcurrentModificationException (com.google.cloud.spanner.AbortedDueToConcurrentModificationException)21 Test (org.junit.Test)21 ParallelIntegrationTest (com.google.cloud.spanner.ParallelIntegrationTest)15 ResultSet (com.google.cloud.spanner.ResultSet)15 ITAbstractSpannerTest (com.google.cloud.spanner.connection.ITAbstractSpannerTest)15 Statement (com.google.cloud.spanner.Statement)9 AsyncResultSet (com.google.cloud.spanner.AsyncResultSet)7 ApiFuture (com.google.api.core.ApiFuture)6 SettableApiFuture (com.google.api.core.SettableApiFuture)6 ITConnection (com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection)6 Timestamp (com.google.cloud.Timestamp)5 CallbackResponse (com.google.cloud.spanner.AsyncResultSet.CallbackResponse)5 Options (com.google.cloud.spanner.Options)5 SpannerApiFutures.get (com.google.cloud.spanner.SpannerApiFutures.get)5 SpannerExceptionFactory (com.google.cloud.spanner.SpannerExceptionFactory)5 Truth.assertThat (com.google.common.truth.Truth.assertThat)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 ExecutorService (java.util.concurrent.ExecutorService)5 Executors (java.util.concurrent.Executors)5 TimeUnit (java.util.concurrent.TimeUnit)5