use of com.google.cloud.spanner.TransactionManager in project java-spanner by googleapis.
the class ITTransactionManagerTest method abortAndRetry.
@SuppressWarnings("resource")
@Test
public void abortAndRetry() throws InterruptedException {
assumeFalse("Emulator does not support more than 1 simultaneous transaction. " + "This test would therefore loop indefinitely on the emulator.", isUsingEmulator());
client.write(Collections.singletonList(Mutation.newInsertBuilder("T").set("K").to("Key3").set("BoolValue").to(true).build()));
try (TransactionManager manager1 = client.transactionManager()) {
TransactionContext txn1 = manager1.begin();
TransactionManager manager2;
TransactionContext txn2;
while (true) {
try {
txn1.readRow("T", Key.of("Key3"), Arrays.asList("K", "BoolValue"));
manager2 = client.transactionManager();
txn2 = manager2.begin();
txn2.readRow("T", Key.of("Key3"), Arrays.asList("K", "BoolValue"));
txn1.buffer(Mutation.newUpdateBuilder("T").set("K").to("Key3").set("BoolValue").to(false).build());
manager1.commit();
break;
} catch (AbortedException e) {
Thread.sleep(e.getRetryDelayInMillis());
// In that case we should just retry without resetting anything.
if (manager1.getState() == TransactionState.ABORTED) {
txn1 = manager1.resetForRetry();
}
}
}
// txn2 should have been aborted.
try {
manager2.commit();
fail("Expected to abort");
} catch (AbortedException e) {
assertThat(manager2.getState()).isEqualTo(TransactionState.ABORTED);
txn2 = manager2.resetForRetry();
}
txn2.buffer(Mutation.newUpdateBuilder("T").set("K").to("Key3").set("BoolValue").to(true).build());
manager2.commit();
Struct row = client.singleUse().readRow("T", Key.of("Key3"), Arrays.asList("K", "BoolValue"));
assertThat(row.getString(0)).isEqualTo("Key3");
assertThat(row.getBoolean(1)).isTrue();
manager2.close();
}
}
use of com.google.cloud.spanner.TransactionManager in project google-cloud-java by googleapis.
the class DatabaseClientSnippets method transactionManager.
/**
* Example of using {@link TransactionManager}.
*/
// [TARGET transactionManager()]
// [VARIABLE my_singer_id]
public void transactionManager(final long singerId) throws InterruptedException {
// [START transactionManager]
try (TransactionManager manager = dbClient.transactionManager()) {
TransactionContext txn = manager.begin();
while (true) {
try {
String column = "FirstName";
Struct row = txn.readRow("Singers", Key.of(singerId), Collections.singleton(column));
String name = row.getString(column);
txn.buffer(Mutation.newUpdateBuilder("Singers").set(column).to(name.toUpperCase()).build());
manager.commit();
break;
} catch (AbortedException e) {
Thread.sleep(e.getRetryDelayInMillis() / 1000);
txn = manager.resetForRetry();
}
}
}
// [END transactionManager]
}
use of com.google.cloud.spanner.TransactionManager in project spring-cloud-gcp by GoogleCloudPlatform.
the class SpannerTransactionManagerTests method testDoGetTransactionAborted.
@Test
void testDoGetTransactionAborted() {
TransactionManager transactionManagerAborted = mock(TransactionManager.class);
when(transactionManagerAborted.getState()).thenReturn(TransactionState.ABORTED);
tx.transactionManager = transactionManager;
TransactionManager transactionManagerNew = mock(TransactionManager.class);
when(transactionManagerNew.getState()).thenReturn(TransactionState.STARTED);
when(this.databaseClient.transactionManager()).thenReturn(transactionManagerNew);
Assert.assertNotEquals("expected a new transaction but got the same one", tx, manager.doGetTransaction());
}
use of com.google.cloud.spanner.TransactionManager in project spring-cloud-gcp by spring-cloud.
the class SpannerTransactionManagerTests method testDoGetTransactionAborted.
@Test
public void testDoGetTransactionAborted() {
TransactionManager transactionManagerAborted = mock(TransactionManager.class);
when(transactionManagerAborted.getState()).thenReturn(TransactionState.ABORTED);
tx.transactionManager = transactionManager;
TransactionManager transactionManagerNew = mock(TransactionManager.class);
when(transactionManagerNew.getState()).thenReturn(TransactionState.STARTED);
when(this.databaseClient.transactionManager()).thenReturn(transactionManagerNew);
Assert.assertNotEquals("expected a new transaction but got the same one", tx, manager.doGetTransaction());
}
use of com.google.cloud.spanner.TransactionManager in project google-cloud-java by GoogleCloudPlatform.
the class DatabaseClientSnippets method transactionManager.
/**
* Example of using {@link TransactionManager}.
*/
// [TARGET transactionManager()]
// [VARIABLE my_singer_id]
public void transactionManager(final long singerId) throws InterruptedException {
// [START transactionManager]
try (TransactionManager manager = dbClient.transactionManager()) {
TransactionContext txn = manager.begin();
while (true) {
try {
String column = "FirstName";
Struct row = txn.readRow("Singers", Key.of(singerId), Collections.singleton(column));
String name = row.getString(column);
txn.buffer(Mutation.newUpdateBuilder("Singers").set(column).to(name.toUpperCase()).build());
manager.commit();
break;
} catch (AbortedException e) {
Thread.sleep(e.getRetryDelayInMillis() / 1000);
txn = manager.resetForRetry();
}
}
}
// [END transactionManager]
}
Aggregations