Search in sources :

Example 6 with TransactionManager

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();
    }
}
Also used : TransactionManager(com.google.cloud.spanner.TransactionManager) TransactionContext(com.google.cloud.spanner.TransactionContext) AbortedException(com.google.cloud.spanner.AbortedException) Struct(com.google.cloud.spanner.Struct) ParallelIntegrationTest(com.google.cloud.spanner.ParallelIntegrationTest) Test(org.junit.Test)

Example 7 with TransactionManager

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]
}
Also used : TransactionManager(com.google.cloud.spanner.TransactionManager) TransactionContext(com.google.cloud.spanner.TransactionContext) AbortedException(com.google.cloud.spanner.AbortedException) Struct(com.google.cloud.spanner.Struct)

Example 8 with 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());
}
Also used : TransactionManager(com.google.cloud.spanner.TransactionManager) Test(org.junit.jupiter.api.Test)

Example 9 with TransactionManager

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());
}
Also used : TransactionManager(com.google.cloud.spanner.TransactionManager) Test(org.junit.Test)

Example 10 with TransactionManager

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]
}
Also used : TransactionManager(com.google.cloud.spanner.TransactionManager) TransactionContext(com.google.cloud.spanner.TransactionContext) AbortedException(com.google.cloud.spanner.AbortedException) Struct(com.google.cloud.spanner.Struct)

Aggregations

TransactionManager (com.google.cloud.spanner.TransactionManager)13 TransactionContext (com.google.cloud.spanner.TransactionContext)11 Test (org.junit.Test)9 AbortedException (com.google.cloud.spanner.AbortedException)8 ParallelIntegrationTest (com.google.cloud.spanner.ParallelIntegrationTest)7 Struct (com.google.cloud.spanner.Struct)4 DatabaseClient (com.google.cloud.spanner.DatabaseClient)2 ResultSet (com.google.cloud.spanner.ResultSet)2 SpannerException (com.google.cloud.spanner.SpannerException)2 SessionNotFoundException (com.google.cloud.spanner.SessionNotFoundException)1 Spanner (com.google.cloud.spanner.Spanner)1 TransactionRunner (com.google.cloud.spanner.TransactionRunner)1 TransactionCallable (com.google.cloud.spanner.TransactionRunner.TransactionCallable)1 ParsedStatement (com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement)1 Test (org.junit.jupiter.api.Test)1