use of com.google.cloud.spanner.connection.Connection in project java-spanner by googleapis.
the class ITAsyncTransactionRetryTest method testAbortWithConcurrentDelete.
@Test
public void testAbortWithConcurrentDelete() {
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.executeUpdateAsync(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (1, 'test 1')"));
connection.executeUpdateAsync(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (2, 'test 2')"));
get(connection.commitAsync());
}
// 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 (AsyncResultSet rs = connection.executeQueryAsync(Statement.of("SELECT * FROM TEST ORDER BY ID"))) {
get(rs.setCallback(executor, resultSet -> {
while (true) {
switch(resultSet.tryNext()) {
case DONE:
return CallbackResponse.DONE;
case NOT_READY:
return CallbackResponse.CONTINUE;
case OK:
break;
}
}
}));
}
// open a new connection and transaction and remove one of the test records
try (ITConnection connection2 = createConnection()) {
connection2.executeUpdateAsync(Statement.of("DELETE FROM TEST WHERE ID=1"));
get(connection2.commitAsync());
}
// 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);
try {
get(connection.executeUpdateAsync(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (3, 'test 3')")));
fail("Missing expected exception");
} catch (AbortedDueToConcurrentModificationException e) {
assertRetryStatistics(1, 1, 0);
}
}
}
use of com.google.cloud.spanner.connection.Connection in project java-spanner by googleapis.
the class ITAsyncTransactionRetryTest method testAbortWithConcurrentInsert.
@Test
public void testAbortWithConcurrentInsert() {
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
AbortInterceptor interceptor = new AbortInterceptor(0);
try (ITConnection connection = createConnection(interceptor, new CountTransactionRetryListener())) {
// insert two test records
connection.executeUpdateAsync(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (1, 'test 1')"));
connection.executeUpdateAsync(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (2, 'test 2')"));
// select the test records and consume the entire result set
try (AsyncResultSet rs = connection.executeQueryAsync(Statement.of("SELECT * FROM TEST ORDER BY ID"))) {
get(rs.setCallback(executor, resultSet -> {
while (true) {
switch(resultSet.tryNext()) {
case DONE:
return CallbackResponse.DONE;
case NOT_READY:
return CallbackResponse.CONTINUE;
case OK:
break;
}
}
}));
}
// open a new connection and transaction and do an additional insert
try (ITConnection connection2 = createConnection()) {
connection2.executeUpdateAsync(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (3, 'test 3')"));
get(connection2.commitAsync());
}
// 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);
ApiFuture<Long> updateCount = connection.executeUpdateAsync(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (4, 'test 4')"));
try {
get(updateCount);
fail("Missing expected exception");
} catch (AbortedDueToConcurrentModificationException e) {
assertRetryStatistics(1, 1, 0);
}
}
}
use of com.google.cloud.spanner.connection.Connection in project pgadapter by GoogleCloudPlatform.
the class MutationWriter method writeToSpanner.
/**
* Write list of mutations to Spanner and reset for next batch
*
* @return count of the number of rows updated.
*/
public int writeToSpanner(ConnectionHandler connectionHandler) {
if (this.mutations.isEmpty()) {
return 0;
}
Connection connection = connectionHandler.getSpannerConnection();
// Write mutation to spanner
connection.write(this.mutations);
// Reset mutations, mutation counter, and batch size count for a new batch
this.mutations = new ArrayList<>();
this.mutationCount = 0;
return this.rowCount;
}
Aggregations