Search in sources :

Example 1 with TransactionContextImpl

use of com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl in project java-spanner by googleapis.

the class TransactionContextImplTest method testCannotCommitTwice.

@Test
public void testCannotCommitTwice() {
    try (TransactionContextImpl context = createContext()) {
        context.commit();
        assertThrows(IllegalStateException.class, () -> context.commit());
    }
}
Also used : TransactionContextImpl(com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl) Test(org.junit.Test)

Example 2 with TransactionContextImpl

use of com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl in project java-spanner by googleapis.

the class TransactionContextImplTest method testCannotBufferAsyncAfterCommit.

@Test
public void testCannotBufferAsyncAfterCommit() {
    try (TransactionContextImpl context = createContext()) {
        context.commit();
        assertThrows(IllegalStateException.class, () -> context.bufferAsync(Mutation.delete("test", KeySet.all())));
    }
}
Also used : TransactionContextImpl(com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl) Test(org.junit.Test)

Example 3 with TransactionContextImpl

use of com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl in project java-spanner by googleapis.

the class TransactionContextImplTest method testReturnCommitStats.

@SuppressWarnings("unchecked")
@Test
public void testReturnCommitStats() {
    ByteString transactionId = ByteString.copyFromUtf8("test");
    try (TransactionContextImpl context = TransactionContextImpl.newBuilder().setSession(session).setRpc(rpc).setTransactionId(transactionId).setOptions(Options.fromTransactionOptions(Options.commitStats())).build()) {
        context.commitAsync();
        CommitRequest request = CommitRequest.newBuilder().setReturnCommitStats(true).setSession(session.getName()).setTransactionId(transactionId).build();
        verify(rpc).commitAsync(Mockito.eq(request), anyMap());
    }
}
Also used : TransactionContextImpl(com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl) CommitRequest(com.google.spanner.v1.CommitRequest) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 4 with TransactionContextImpl

use of com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl in project java-spanner by googleapis.

the class TransactionContextImplTest method testCannotBufferIterableAfterCommit.

@Test
public void testCannotBufferIterableAfterCommit() {
    try (TransactionContextImpl context = createContext()) {
        context.commit();
        assertThrows(IllegalStateException.class, () -> context.buffer(Collections.singleton(Mutation.delete("test", KeySet.all()))));
    }
}
Also used : TransactionContextImpl(com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl) Test(org.junit.Test)

Example 5 with TransactionContextImpl

use of com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl in project java-spanner by googleapis.

the class TransactionContextImplTest method batchDml.

@SuppressWarnings("unchecked")
private void batchDml(int status) {
    SessionImpl session = mock(SessionImpl.class);
    when(session.getName()).thenReturn("test");
    SpannerRpc rpc = mock(SpannerRpc.class);
    ExecuteBatchDmlResponse response = ExecuteBatchDmlResponse.newBuilder().setStatus(Status.newBuilder().setCode(status).build()).build();
    Statement statement = Statement.of("UPDATE FOO SET BAR=1");
    when(rpc.executeBatchDml(Mockito.any(ExecuteBatchDmlRequest.class), Mockito.anyMap())).thenReturn(response);
    try (TransactionContextImpl impl = TransactionContextImpl.newBuilder().setSession(session).setRpc(rpc).setTransactionId(ByteString.copyFromUtf8("test")).setOptions(Options.fromTransactionOptions()).build()) {
        impl.batchUpdate(Collections.singletonList(statement));
    }
}
Also used : TransactionContextImpl(com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl) ExecuteBatchDmlResponse(com.google.spanner.v1.ExecuteBatchDmlResponse) SpannerRpc(com.google.cloud.spanner.spi.v1.SpannerRpc) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest)

Aggregations

TransactionContextImpl (com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl)11 Test (org.junit.Test)8 ByteString (com.google.protobuf.ByteString)3 CommitRequest (com.google.spanner.v1.CommitRequest)3 ExecuteBatchDmlRequest (com.google.spanner.v1.ExecuteBatchDmlRequest)3 SessionPoolTransactionContext (com.google.cloud.spanner.SessionPool.SessionPoolTransactionContext)2 SpannerRpc (com.google.cloud.spanner.spi.v1.SpannerRpc)2 CommitResponse (com.google.spanner.v1.CommitResponse)2 ExecuteBatchDmlResponse (com.google.spanner.v1.ExecuteBatchDmlResponse)2 RollbackRequest (com.google.spanner.v1.RollbackRequest)2 Span (io.opencensus.trace.Span)2 ApiFutures (com.google.api.core.ApiFutures)1 Timestamp (com.google.cloud.Timestamp)1 NUM_IN_USE_SESSIONS (com.google.cloud.spanner.MetricRegistryConstants.NUM_IN_USE_SESSIONS)1 NUM_READ_SESSIONS (com.google.cloud.spanner.MetricRegistryConstants.NUM_READ_SESSIONS)1 NUM_SESSIONS_BEING_PREPARED (com.google.cloud.spanner.MetricRegistryConstants.NUM_SESSIONS_BEING_PREPARED)1 NUM_WRITE_SESSIONS (com.google.cloud.spanner.MetricRegistryConstants.NUM_WRITE_SESSIONS)1 SPANNER_LABEL_KEYS (com.google.cloud.spanner.MetricRegistryConstants.SPANNER_LABEL_KEYS)1 SPANNER_LABEL_KEYS_WITH_TYPE (com.google.cloud.spanner.MetricRegistryConstants.SPANNER_LABEL_KEYS_WITH_TYPE)1 FakeMetricRegistry (com.google.cloud.spanner.MetricRegistryTestUtils.FakeMetricRegistry)1