Search in sources :

Example 11 with TransactionManager

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

Example 12 with TransactionManager

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

Example 13 with TransactionManager

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

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