Search in sources :

Example 6 with TransactionOptions

use of com.google.firestore.v1beta1.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.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) TransactionOptions(com.google.spanner.v1.TransactionOptions) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 7 with TransactionOptions

use of com.google.firestore.v1beta1.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);
    }
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) TransactionOptions(com.google.spanner.v1.TransactionOptions) RequestOptions(com.google.spanner.v1.RequestOptions) RequestOptions(com.google.spanner.v1.RequestOptions) ArrayList(java.util.ArrayList) Span(io.opencensus.trace.Span) Scope(io.opencensus.common.Scope)

Example 8 with TransactionOptions

use of com.google.firestore.v1beta1.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);
    }
}
Also used : Transaction(com.google.spanner.v1.Transaction) TransactionOptions(com.google.spanner.v1.TransactionOptions) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) Session(com.google.spanner.v1.Session)

Example 9 with TransactionOptions

use of com.google.firestore.v1beta1.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();
}
Also used : GRPCFirebaseClientFactory(org.roguewave.grpc.util.GRPCFirebaseClientFactory) FirestoreBlockingStub(com.google.firestore.v1beta1.FirestoreGrpc.FirestoreBlockingStub) TransactionOptions(com.google.firestore.v1beta1.TransactionOptions) BeginTransactionRequest(com.google.firestore.v1beta1.BeginTransactionRequest) Menu(org.roguewave.grpc.util.gfx.Menu) BeginTransactionResponse(com.google.firestore.v1beta1.BeginTransactionResponse)

Example 10 with TransactionOptions

use of com.google.firestore.v1beta1.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());
}
Also used : PartitionResponse(com.google.spanner.v1.PartitionResponse) TransactionOptions(com.google.spanner.v1.TransactionOptions) TransactionSelector(com.google.spanner.v1.TransactionSelector) PartitionQueryRequest(com.google.spanner.v1.PartitionQueryRequest) SpannerClient(com.google.cloud.spanner.v1.SpannerClient) Session(com.google.spanner.v1.Session)

Aggregations

TransactionOptions (com.google.spanner.v1.TransactionOptions)16 Test (org.junit.Test)13 TransactionSelector (com.google.spanner.v1.TransactionSelector)6 Session (com.google.spanner.v1.Session)5 ArrayList (java.util.ArrayList)5 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)4 AbstractMessage (com.google.protobuf.AbstractMessage)4 ByteString (com.google.protobuf.ByteString)4 Mutation (com.google.spanner.v1.Mutation)4 PartitionResponse (com.google.spanner.v1.PartitionResponse)4 SessionName (com.google.spanner.v1.SessionName)4 StatusRuntimeException (io.grpc.StatusRuntimeException)4 TransactionOptions (com.google.datastore.v1.TransactionOptions)3 BeginTransactionRequest (com.google.spanner.v1.BeginTransactionRequest)3 CommitRequest (com.google.spanner.v1.CommitRequest)3 PartitionQueryRequest (com.google.spanner.v1.PartitionQueryRequest)3 Transaction (com.google.spanner.v1.Transaction)3 BeginTransactionRequest (com.google.datastore.v1.BeginTransactionRequest)2 CommitRequest (com.google.datastore.v1.CommitRequest)2 GcpManagedChannel (com.google.grpc.gcp.GcpManagedChannel)2