use of com.google.spanner.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.spanner.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.spanner.v1.TransactionOptions in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerProbes method transactionProber.
/**
* Probe to test BeginTransaction, Commit and Rollback grpc from Spanner stub.
*/
public static void transactionProber(SpannerGrpc.SpannerBlockingStub stub) {
Session session = null;
try {
session = stub.createSession(CreateSessionRequest.newBuilder().setDatabase(DATABASE).build());
// Probing begin transaction call.
TransactionOptions options = TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.getDefaultInstance()).build();
BeginTransactionRequest request = BeginTransactionRequest.newBuilder().setSession(session.getName()).setOptions(options).build();
Transaction txn = stub.beginTransaction(request);
// Probing commit call.
stub.commit(CommitRequest.newBuilder().setSession(session.getName()).setTransactionId(txn.getId()).build());
// Probing rollback call.
txn = stub.beginTransaction(request);
stub.rollback(RollbackRequest.newBuilder().setSession(session.getName()).setTransactionId(txn.getId()).build());
} finally {
deleteSession(stub, session);
}
}
use of com.google.spanner.v1.TransactionOptions in project grpc-gcp-java by GoogleCloudPlatform.
the class BeginTransaction method beginTransactionCall.
public void beginTransactionCall() {
FirestoreBlockingStub blockingStub = new GRPCFirebaseClientFactory().createFirebaseClient().getBlockingStub();
TransactionOptions tOpts = TransactionOptions.newBuilder().build();
BeginTransactionRequest beginTransactionRequest = BeginTransactionRequest.newBuilder().setDatabase("projects/firestoretestclient/databases/(default)").setOptions(tOpts).build();
BeginTransactionResponse response;
try {
response = blockingStub.beginTransaction(beginTransactionRequest);
} catch (Exception e) {
System.out.println("Error during call: " + e.getMessage() + e.getCause());
return;
}
System.out.println("Began Transaction: " + response.getTransaction().toString());
Main.transactionId = response.getTransaction();
Menu menu = new Menu();
menu.draw();
}
use of com.google.spanner.v1.TransactionOptions in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerClientV1TestCases method testPartitionQuery.
void testPartitionQuery() throws InterruptedException {
System.out.println("\nTestPartitionQuery");
SpannerClient client = getClient();
Session session = client.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
TransactionOptions options = TransactionOptions.newBuilder().setReadOnly(TransactionOptions.ReadOnly.getDefaultInstance()).build();
TransactionSelector selector = TransactionSelector.newBuilder().setBegin(options).build();
PartitionQueryRequest request = PartitionQueryRequest.newBuilder().setSession(session.getName()).setSql("select * FROM " + LARGE_TABLE).setTransaction(selector).build();
RpcCall<PartitionQueryRequest, PartitionResponse> rpcCall = (PartitionQueryRequest req) -> client.partitionQuery(req);
doTestBlocking(request, rpcCall);
cleanUpClient(client, session.getName());
}
Aggregations