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