use of com.google.datastore.v1.TransactionOptions in project java-spanner by googleapis.
the class SpannerClientTest method beginTransactionExceptionTest2.
@Test
public void beginTransactionExceptionTest2() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
mockSpanner.addException(exception);
try {
String session = "session1984987798";
TransactionOptions options = TransactionOptions.newBuilder().build();
client.beginTransaction(session, options);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception.
}
}
use of com.google.datastore.v1.TransactionOptions in project java-spanner by googleapis.
the class SessionImpl method writeAtLeastOnceWithOptions.
@Override
public CommitResponse writeAtLeastOnceWithOptions(Iterable<Mutation> mutations, TransactionOption... transactionOptions) throws SpannerException {
setActive(null);
Options commitRequestOptions = Options.fromTransactionOptions(transactionOptions);
List<com.google.spanner.v1.Mutation> mutationsProto = new ArrayList<>();
Mutation.toProto(mutations, mutationsProto);
final CommitRequest.Builder requestBuilder = CommitRequest.newBuilder().setSession(name).setReturnCommitStats(Options.fromTransactionOptions(transactionOptions).withCommitStats()).addAllMutations(mutationsProto).setSingleUseTransaction(TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.getDefaultInstance()));
if (commitRequestOptions.hasPriority() || commitRequestOptions.hasTag()) {
RequestOptions.Builder requestOptionsBuilder = RequestOptions.newBuilder();
if (commitRequestOptions.hasPriority()) {
requestOptionsBuilder.setPriority(commitRequestOptions.priority());
}
if (commitRequestOptions.hasTag()) {
requestOptionsBuilder.setTransactionTag(commitRequestOptions.tag());
}
requestBuilder.setRequestOptions(requestOptionsBuilder.build());
}
Span span = tracer.spanBuilder(SpannerImpl.COMMIT).startSpan();
try (Scope s = tracer.withSpan(span)) {
com.google.spanner.v1.CommitResponse response = spanner.getRpc().commit(requestBuilder.build(), this.options);
return new CommitResponse(response);
} catch (RuntimeException e) {
TraceUtil.setWithFailure(span, e);
throw e;
} finally {
span.end(TraceUtil.END_SPAN_OPTIONS);
}
}
use of com.google.datastore.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.datastore.v1.TransactionOptions in project java-datastore by googleapis.
the class ITDatastoreTest 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.datastore.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