Search in sources :

Example 1 with TransactionManager

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);
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) TransactionContext(com.google.cloud.spanner.TransactionContext) TransactionRunner(com.google.cloud.spanner.TransactionRunner) TransactionCallable(com.google.cloud.spanner.TransactionRunner.TransactionCallable) TransactionManager(com.google.cloud.spanner.TransactionManager) Spanner(com.google.cloud.spanner.Spanner)

Example 2 with TransactionManager

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

Example 3 with TransactionManager

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

Example 4 with TransactionManager

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

Example 5 with TransactionManager

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();
            }
        }
    }
}
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)

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