Search in sources :

Example 1 with BeginTransactionRequest

use of com.google.spanner.v1.BeginTransactionRequest in project google-cloud-java by GoogleCloudPlatform.

the class SessionImplTest method multiUseReadOnlyTransactionReturnsEmptyTransactionMetadata.

@Test
public void multiUseReadOnlyTransactionReturnsEmptyTransactionMetadata() {
    Transaction txnMetadata = Transaction.newBuilder().setId(ByteString.copyFromUtf8("x")).build();
    PartialResultSet resultSet = PartialResultSet.newBuilder().setMetadata(newMetadata(Type.struct(Type.StructField.of("C", Type.string())))).build();
    Mockito.when(rpc.beginTransaction(Mockito.<BeginTransactionRequest>any(), Mockito.eq(options))).thenReturn(txnMetadata);
    mockRead(resultSet);
    ReadOnlyTransaction txn = session.readOnlyTransaction(TimestampBound.strong());
    expectedException.expect(SpannerMatchers.isSpannerException(ErrorCode.INTERNAL));
    txn.readRow("Dummy", Key.of(), Arrays.asList("C"));
}
Also used : Transaction(com.google.spanner.v1.Transaction) PartialResultSet(com.google.spanner.v1.PartialResultSet) Test(org.junit.Test)

Example 2 with BeginTransactionRequest

use of com.google.spanner.v1.BeginTransactionRequest in project java-spanner by googleapis.

the class MockSpannerServiceImpl method beginTransaction.

@Override
public void beginTransaction(BeginTransactionRequest request, StreamObserver<Transaction> responseObserver) {
    requests.add(request);
    Preconditions.checkNotNull(request.getSession());
    Session session = sessions.get(request.getSession());
    if (session == null) {
        setSessionNotFound(request.getSession(), responseObserver);
        return;
    }
    sessionLastUsed.put(session.getName(), Instant.now());
    try {
        beginTransactionExecutionTime.simulateExecutionTime(exceptions, stickyGlobalExceptions, freezeLock);
        Transaction transaction = beginTransaction(session, request.getOptions());
        responseObserver.onNext(transaction);
        responseObserver.onCompleted();
    } catch (StatusRuntimeException t) {
        responseObserver.onError(t);
    } catch (Throwable t) {
        responseObserver.onError(Status.INTERNAL.asRuntimeException());
    }
}
Also used : Transaction(com.google.spanner.v1.Transaction) StatusRuntimeException(io.grpc.StatusRuntimeException) Session(com.google.spanner.v1.Session)

Example 3 with BeginTransactionRequest

use of com.google.spanner.v1.BeginTransactionRequest in project java-spanner by googleapis.

the class SpannerClientTest method beginTransactionTest.

@Test
public void beginTransactionTest() throws Exception {
    Transaction expectedResponse = Transaction.newBuilder().setId(ByteString.EMPTY).setReadTimestamp(Timestamp.newBuilder().build()).build();
    mockSpanner.addResponse(expectedResponse);
    SessionName session = SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]");
    TransactionOptions options = TransactionOptions.newBuilder().build();
    Transaction actualResponse = client.beginTransaction(session, options);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockSpanner.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    BeginTransactionRequest actualRequest = ((BeginTransactionRequest) actualRequests.get(0));
    Assert.assertEquals(session.toString(), actualRequest.getSession());
    Assert.assertEquals(options, actualRequest.getOptions());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) Transaction(com.google.spanner.v1.Transaction) TransactionOptions(com.google.spanner.v1.TransactionOptions) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) SessionName(com.google.spanner.v1.SessionName) Test(org.junit.Test)

Example 4 with BeginTransactionRequest

use of com.google.spanner.v1.BeginTransactionRequest in project java-spanner by googleapis.

the class SpannerClientTest method beginTransactionTest2.

@Test
public void beginTransactionTest2() throws Exception {
    Transaction expectedResponse = Transaction.newBuilder().setId(ByteString.EMPTY).setReadTimestamp(Timestamp.newBuilder().build()).build();
    mockSpanner.addResponse(expectedResponse);
    String session = "session1984987798";
    TransactionOptions options = TransactionOptions.newBuilder().build();
    Transaction actualResponse = client.beginTransaction(session, options);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockSpanner.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    BeginTransactionRequest actualRequest = ((BeginTransactionRequest) actualRequests.get(0));
    Assert.assertEquals(session, actualRequest.getSession());
    Assert.assertEquals(options, actualRequest.getOptions());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) Transaction(com.google.spanner.v1.Transaction) TransactionOptions(com.google.spanner.v1.TransactionOptions) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 5 with BeginTransactionRequest

use of com.google.spanner.v1.BeginTransactionRequest in project java-spanner by googleapis.

the class PartitionedDmlTransaction method initTransaction.

private ByteString initTransaction() {
    final BeginTransactionRequest request = BeginTransactionRequest.newBuilder().setSession(session.getName()).setOptions(TransactionOptions.newBuilder().setPartitionedDml(TransactionOptions.PartitionedDml.getDefaultInstance())).build();
    Transaction tx = rpc.beginTransaction(request, session.getOptions());
    if (tx.getId().isEmpty()) {
        throw SpannerExceptionFactory.newSpannerException(ErrorCode.INTERNAL, "Failed to init transaction, missing transaction id\n" + session.getName());
    }
    return tx.getId();
}
Also used : Transaction(com.google.spanner.v1.Transaction) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest)

Aggregations

Transaction (com.google.spanner.v1.Transaction)11 Test (org.junit.Test)10 BeginTransactionRequest (com.google.spanner.v1.BeginTransactionRequest)7 BeginTransactionRequest (com.google.datastore.v1.BeginTransactionRequest)5 ByteString (com.google.protobuf.ByteString)5 ExecutionException (java.util.concurrent.ExecutionException)5 DatastoreException (com.google.datastore.v1.client.DatastoreException)4 Session (com.google.spanner.v1.Session)4 BeginTransactionRequest (com.google.firestore.v1.BeginTransactionRequest)3 BeginTransactionResponse (com.google.firestore.v1.BeginTransactionResponse)3 AbstractMessage (com.google.protobuf.AbstractMessage)3 PartialResultSet (com.google.spanner.v1.PartialResultSet)3 TransactionOptions (com.google.spanner.v1.TransactionOptions)3 CommitRequest (com.google.firestore.v1.CommitRequest)2 CommitResponse (com.google.firestore.v1.CommitResponse)2 FirestoreGrpc (com.google.firestore.v1.FirestoreGrpc)2 RollbackRequest (com.google.firestore.v1.RollbackRequest)2 TransactionOptions (com.google.firestore.v1.TransactionOptions)2 Empty (com.google.protobuf.Empty)2 Nullable (org.springframework.lang.Nullable)2