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);
}
}
Aggregations