use of com.google.cloud.spanner.TransactionManager in project java-spanner by googleapis.
the class ReadWriteTransactionTest method testRetry.
@Test
public void testRetry() {
for (RetryResults results : RetryResults.values()) {
String sql1 = "UPDATE FOO SET BAR=1 WHERE BAZ>=100 AND BAZ<200";
String sql2 = "UPDATE FOO SET BAR=2 WHERE BAZ>=200 AND BAZ<300";
DatabaseClient client = mock(DatabaseClient.class);
ParsedStatement update1 = mock(ParsedStatement.class);
when(update1.getType()).thenReturn(StatementType.UPDATE);
when(update1.isUpdate()).thenReturn(true);
when(update1.getStatement()).thenReturn(Statement.of(sql1));
ParsedStatement update2 = mock(ParsedStatement.class);
when(update2.getType()).thenReturn(StatementType.UPDATE);
when(update2.isUpdate()).thenReturn(true);
when(update2.getStatement()).thenReturn(Statement.of(sql2));
TransactionManager txManager = mock(TransactionManager.class);
TransactionContext txContext1 = mock(TransactionContext.class);
when(txManager.begin()).thenReturn(txContext1);
when(txManager.getState()).thenReturn(null, TransactionState.STARTED);
when(client.transactionManager()).thenReturn(txManager);
when(txContext1.executeUpdate(Statement.of(sql1))).thenReturn(90L);
when(txContext1.executeUpdate(Statement.of(sql2))).thenReturn(80L);
TransactionContext txContext2 = mock(TransactionContext.class);
when(txManager.resetForRetry()).thenReturn(txContext2);
when(client.transactionManager()).thenReturn(txManager);
if (results == RetryResults.SAME) {
when(txContext2.executeUpdate(Statement.of(sql1))).thenReturn(90L);
when(txContext2.executeUpdate(Statement.of(sql2))).thenReturn(80L);
} else if (results == RetryResults.DIFFERENT) {
when(txContext2.executeUpdate(Statement.of(sql1))).thenReturn(90L);
when(txContext2.executeUpdate(Statement.of(sql2))).thenReturn(90L);
}
// first abort, then do nothing
doThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.ABORTED, "commit aborted", createAbortedExceptionWithMinimalRetry())).doNothing().when(txManager).commit();
ReadWriteTransaction subject = ReadWriteTransaction.newBuilder().setRetryAbortsInternally(true).setTransactionRetryListeners(Collections.emptyList()).setDatabaseClient(client).withStatementExecutor(new StatementExecutor()).build();
subject.executeUpdateAsync(update1);
subject.executeUpdateAsync(update2);
boolean expectedException = false;
try {
get(subject.commitAsync());
} catch (SpannerException e) {
if (results == RetryResults.DIFFERENT && e.getErrorCode() == ErrorCode.ABORTED) {
// expected
expectedException = true;
} else {
throw e;
}
}
assertThat(expectedException, is(results == RetryResults.DIFFERENT));
}
}
use of com.google.cloud.spanner.TransactionManager in project java-spanner by googleapis.
the class ITClosedSessionTest method testTransactionManager.
@Test
public void testTransactionManager() throws InterruptedException {
client.invalidateNextSession();
for (int run = 0; run < 2; run++) {
try (TransactionManager manager = client.transactionManager()) {
TransactionContext txn = manager.begin();
try {
while (true) {
for (int i = 0; i < 2; i++) {
try (ResultSet rs = txn.executeQuery(Statement.of("SELECT 1"))) {
assertThat(rs.next()).isTrue();
assertThat(rs.getLong(0)).isEqualTo(1L);
assertThat(rs.next()).isFalse();
}
}
manager.commit();
break;
}
} catch (AbortedException e) {
Thread.sleep(e.getRetryDelayInMillis());
txn = manager.resetForRetry();
}
}
}
}
use of com.google.cloud.spanner.TransactionManager in project java-spanner by googleapis.
the class ITTransactionManagerTest method rollback.
@SuppressWarnings("resource")
@Test
public void rollback() throws InterruptedException {
try (TransactionManager manager = client.transactionManager()) {
TransactionContext txn = manager.begin();
while (true) {
txn.buffer(Mutation.newInsertBuilder("T").set("K").to("Key2").set("BoolValue").to(true).build());
try {
manager.rollback();
break;
} catch (AbortedException e) {
Thread.sleep(e.getRetryDelayInMillis());
txn = manager.resetForRetry();
}
}
assertThat(manager.getState()).isEqualTo(TransactionState.ROLLED_BACK);
// Row should not have been inserted.
assertThat(client.singleUse().readRow("T", Key.of("Key2"), Arrays.asList("K", "BoolValue"))).isNull();
}
}
Aggregations