use of com.google.cloud.spanner.TransactionManager in project java-spanner by googleapis.
the class AutocommitDmlModeTest method createConnection.
@SuppressWarnings("unchecked")
private ConnectionImpl createConnection(ConnectionOptions options) {
dbClient = mock(DatabaseClient.class);
when(dbClient.getDialect()).thenReturn(Dialect.GOOGLE_STANDARD_SQL);
txContext = mock(TransactionContext.class);
Spanner spanner = mock(Spanner.class);
SpannerPool spannerPool = mock(SpannerPool.class);
when(spannerPool.getSpanner(any(ConnectionOptions.class), any(ConnectionImpl.class))).thenReturn(spanner);
DdlClient ddlClient = mock(DdlClient.class);
TransactionRunner txRunner = mock(TransactionRunner.class);
when(dbClient.readWriteTransaction()).thenReturn(txRunner);
when(txRunner.run(any(TransactionCallable.class))).thenAnswer(invocation -> {
TransactionCallable<Long> callable = (TransactionCallable<Long>) invocation.getArguments()[0];
return callable.run(txContext);
});
TransactionManager txManager = mock(TransactionManager.class);
when(txManager.begin()).thenReturn(txContext);
when(dbClient.transactionManager()).thenReturn(txManager);
return new ConnectionImpl(options, spannerPool, ddlClient, dbClient);
}
use of com.google.cloud.spanner.TransactionManager in project java-spanner by googleapis.
the class ITClosedSessionTest method testTransactionManagerNoRecreation.
@Test
public void testTransactionManagerNoRecreation() {
client.setAllowSessionReplacing(false);
client.invalidateNextSession();
try (TransactionManager manager = client.transactionManager()) {
TransactionContext txn = manager.begin();
while (true) {
try (ResultSet rs = txn.executeQuery(Statement.of("SELECT 1"))) {
rs.next();
fail("Expected exception");
}
}
} catch (SessionNotFoundException ex) {
assertNotNull(ex.getMessage());
}
}
use of com.google.cloud.spanner.TransactionManager in project java-spanner by googleapis.
the class ITTransactionManagerTest method invalidInsert.
@SuppressWarnings("resource")
@Test
public void invalidInsert() throws InterruptedException {
try (TransactionManager manager = client.transactionManager()) {
TransactionContext txn = manager.begin();
while (true) {
txn.buffer(Mutation.newInsertBuilder("InvalidTable").set("K").to("Key1").set("BoolValue").to(true).build());
try {
manager.commit();
fail("Expected exception");
} catch (AbortedException e) {
Thread.sleep(e.getRetryDelayInMillis());
txn = manager.resetForRetry();
} catch (SpannerException e) {
// expected
break;
}
}
assertThat(manager.getState()).isEqualTo(TransactionState.COMMIT_FAILED);
// We cannot retry for non aborted errors.
try {
manager.resetForRetry();
fail("Expected exception");
} catch (IllegalStateException ex) {
assertNotNull(ex.getMessage());
}
}
}
use of com.google.cloud.spanner.TransactionManager in project java-spanner by googleapis.
the class ITTransactionManagerTest method testTransactionManagerReturnsCommitStats.
@SuppressWarnings("resource")
@Test
public void testTransactionManagerReturnsCommitStats() throws InterruptedException {
assumeFalse("Emulator does not return commit statistics", isUsingEmulator());
try (TransactionManager manager = client.transactionManager(Options.commitStats())) {
TransactionContext transaction = manager.begin();
while (true) {
transaction.buffer(Mutation.newInsertBuilder("T").set("K").to("KeyCommitStats").set("BoolValue").to(true).build());
try {
manager.commit();
assertNotNull(manager.getCommitResponse().getCommitStats());
assertEquals(2L, manager.getCommitResponse().getCommitStats().getMutationCount());
break;
} catch (AbortedException e) {
Thread.sleep(e.getRetryDelayInMillis());
transaction = manager.resetForRetry();
}
}
}
}
use of com.google.cloud.spanner.TransactionManager in project java-spanner by googleapis.
the class ITTransactionManagerTest method simpleInsert.
@SuppressWarnings("resource")
@Test
public void simpleInsert() throws InterruptedException {
try (TransactionManager manager = client.transactionManager()) {
TransactionContext txn = manager.begin();
while (true) {
assertThat(manager.getState()).isEqualTo(TransactionState.STARTED);
txn.buffer(Mutation.newInsertBuilder("T").set("K").to("Key1").set("BoolValue").to(true).build());
try {
manager.commit();
assertThat(manager.getState()).isEqualTo(TransactionState.COMMITTED);
Struct row = client.singleUse().readRow("T", Key.of("Key1"), Arrays.asList("K", "BoolValue"));
assertThat(row.getString(0)).isEqualTo("Key1");
assertThat(row.getBoolean(1)).isTrue();
break;
} catch (AbortedException e) {
Thread.sleep(e.getRetryDelayInMillis());
txn = manager.resetForRetry();
}
}
}
}
Aggregations