use of com.google.spanner.v1.TransactionOptions in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerTestCases method testPartitionQueryAsync.
void testPartitionQueryAsync() throws InterruptedException {
System.out.println("\nTestPartitionQueryAsync");
ManagedChannel channel = getChannel();
SpannerStub stub = getStub(channel);
AsyncResponseObserver<Session> sessionObs = new AsyncResponseObserver<>();
stub.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build(), sessionObs);
TransactionOptions options = TransactionOptions.newBuilder().setReadOnly(TransactionOptions.ReadOnly.getDefaultInstance()).build();
TransactionSelector selector = TransactionSelector.newBuilder().setBegin(options).build();
PartitionQueryRequest request = PartitionQueryRequest.newBuilder().setSession(sessionObs.get().getName()).setSql("select * FROM " + LARGE_TABLE).setTransaction(selector).build();
AsyncCall<PartitionQueryRequest, PartitionResponse> asyncCall = (PartitionQueryRequest req, AsyncResponseObserver<PartitionResponse> resp) -> stub.partitionQuery(req, resp);
doTestAsync(channel, request, asyncCall);
deleteAndCloseAsync(stub, channel, sessionObs.get().getName());
}
use of com.google.spanner.v1.TransactionOptions in project java-datastore by googleapis.
the class ITDatastoreTest method testRunInTransactionReadWrite.
@Test
public void testRunInTransactionReadWrite() {
final Entity entity1 = Entity.newBuilder(ENTITY1).clear().setNull("bla").build();
Datastore.TransactionCallable<Integer> callable1 = new Datastore.TransactionCallable<Integer>() {
private Integer attempts = 1;
@Override
public Integer run(DatastoreReaderWriter transaction) {
transaction.update(entity1);
if (attempts < 2) {
++attempts;
throw new DatastoreException(10, "", "ABORTED", false, null);
}
return attempts;
}
};
int result = DATASTORE.runInTransaction(callable1);
assertEquals(result, 2);
final Entity entity2 = Entity.newBuilder(ENTITY2).clear().setNull("bla").build();
Datastore.TransactionCallable<Integer> callable2 = new Datastore.TransactionCallable<Integer>() {
private Integer attempts = 1;
@Override
public Integer run(DatastoreReaderWriter transaction) {
transaction.update(entity2);
if (attempts < 2) {
++attempts;
throw new DatastoreException(10, "", "ABORTED", false, null);
}
return attempts;
}
};
TransactionOptions readOnlyOptions = TransactionOptions.newBuilder().setReadOnly(TransactionOptions.ReadOnly.getDefaultInstance()).build();
try {
DATASTORE.runInTransaction(callable2, readOnlyOptions);
fail("Expecting a failure");
} catch (DatastoreException expected) {
assertEquals(3, ((DatastoreException) expected.getCause()).getCode());
}
}
use of com.google.spanner.v1.TransactionOptions in project java-datastore by googleapis.
the class DatastoreTest method testRunInTransactionWithReadWriteOption.
@Test
public void testRunInTransactionWithReadWriteOption() {
EasyMock.expect(rpcMock.beginTransaction(EasyMock.anyObject(BeginTransactionRequest.class))).andReturn(BeginTransactionResponse.getDefaultInstance());
EasyMock.expect(rpcMock.rollback(EasyMock.anyObject(RollbackRequest.class))).andReturn(RollbackResponse.getDefaultInstance()).once();
EasyMock.expect(rpcMock.beginTransaction(EasyMock.anyObject(BeginTransactionRequest.class))).andReturn(BeginTransactionResponse.getDefaultInstance());
EasyMock.expect(rpcMock.commit(EasyMock.anyObject(CommitRequest.class))).andReturn(CommitResponse.newBuilder().build());
EasyMock.replay(rpcFactoryMock, rpcMock);
Datastore mockDatastore = rpcMockOptions.getService();
Datastore.TransactionCallable<Integer> callable = new Datastore.TransactionCallable<Integer>() {
private Integer attempts = 1;
@Override
public Integer run(DatastoreReaderWriter transaction) {
if (attempts < 2) {
++attempts;
throw new DatastoreException(10, "", "ABORTED", false, null);
}
return attempts;
}
};
TransactionOptions options = TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.getDefaultInstance()).build();
Integer result = mockDatastore.runInTransaction(callable, options);
EasyMock.verify(rpcFactoryMock, rpcMock);
assertEquals(2, result.intValue());
}
use of com.google.spanner.v1.TransactionOptions in project java-spanner by googleapis.
the class MockSpannerServiceImpl method beginTransaction.
private Transaction beginTransaction(Session session, TransactionOptions options) {
Transaction.Builder builder = Transaction.newBuilder().setId(generateTransactionName(session.getName()));
if (options != null && options.getModeCase() == ModeCase.READ_ONLY) {
setReadTimestamp(options, builder);
}
Transaction transaction = builder.build();
transactions.put(transaction.getId(), transaction);
transactionsStarted.add(transaction.getId());
isPartitionedDmlTransaction.put(transaction.getId(), options.getModeCase() == ModeCase.PARTITIONED_DML);
if (abortNextTransaction.getAndSet(false)) {
markAbortedTransaction(transaction.getId());
}
return transaction;
}
use of com.google.spanner.v1.TransactionOptions in project java-spanner by googleapis.
the class SpannerClientTest method commitExceptionTest2.
@Test
public void commitExceptionTest2() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
mockSpanner.addException(exception);
try {
SessionName session = SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]");
TransactionOptions singleUseTransaction = TransactionOptions.newBuilder().build();
List<Mutation> mutations = new ArrayList<>();
client.commit(session, singleUseTransaction, mutations);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception.
}
}
Aggregations