Search in sources :

Example 11 with TransactionOptions

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());
}
Also used : PartitionResponse(com.google.spanner.v1.PartitionResponse) SpannerStub(com.google.spanner.v1.SpannerGrpc.SpannerStub) TransactionOptions(com.google.spanner.v1.TransactionOptions) TransactionSelector(com.google.spanner.v1.TransactionSelector) PartitionQueryRequest(com.google.spanner.v1.PartitionQueryRequest) ManagedChannel(io.grpc.ManagedChannel) GcpManagedChannel(com.google.grpc.gcp.GcpManagedChannel) Session(com.google.spanner.v1.Session)

Example 12 with TransactionOptions

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());
    }
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) ProjectionEntity(com.google.cloud.datastore.ProjectionEntity) Datastore(com.google.cloud.datastore.Datastore) DatastoreReaderWriter(com.google.cloud.datastore.DatastoreReaderWriter) TransactionOptions(com.google.datastore.v1.TransactionOptions) DatastoreException(com.google.cloud.datastore.DatastoreException) Test(org.junit.Test)

Example 13 with TransactionOptions

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());
}
Also used : CommitRequest(com.google.datastore.v1.CommitRequest) BeginTransactionRequest(com.google.datastore.v1.BeginTransactionRequest) TransactionOptions(com.google.datastore.v1.TransactionOptions) Test(org.junit.Test)

Example 14 with TransactionOptions

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;
}
Also used : Transaction(com.google.spanner.v1.Transaction)

Example 15 with TransactionOptions

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.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) TransactionOptions(com.google.spanner.v1.TransactionOptions) StatusRuntimeException(io.grpc.StatusRuntimeException) ArrayList(java.util.ArrayList) Mutation(com.google.spanner.v1.Mutation) SessionName(com.google.spanner.v1.SessionName) Test(org.junit.Test)

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 Transaction (com.google.spanner.v1.Transaction)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 BeginTransactionRequest (com.google.datastore.v1.BeginTransactionRequest)2 CommitRequest (com.google.datastore.v1.CommitRequest)2 GcpManagedChannel (com.google.grpc.gcp.GcpManagedChannel)2