Search in sources :

Example 6 with TransactionOptions

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.
    }
}
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.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);
    }
}
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.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());
    }
}
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 9 with TransactionOptions

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());
}
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 10 with TransactionOptions

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.
    }
}
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

Test (org.junit.Test)10 TransactionOptions (com.google.spanner.v1.TransactionOptions)9 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 SessionName (com.google.spanner.v1.SessionName)4 StatusRuntimeException (io.grpc.StatusRuntimeException)4 CommitRequest (com.google.spanner.v1.CommitRequest)3 TransactionOptions (com.google.datastore.v1.TransactionOptions)2 BeginTransactionRequest (com.google.spanner.v1.BeginTransactionRequest)2 CommitResponse (com.google.spanner.v1.CommitResponse)2 Transaction (com.google.spanner.v1.Transaction)2 Datastore (com.google.cloud.datastore.Datastore)1 DatastoreException (com.google.cloud.datastore.DatastoreException)1 DatastoreReaderWriter (com.google.cloud.datastore.DatastoreReaderWriter)1 Entity (com.google.cloud.datastore.Entity)1 FullEntity (com.google.cloud.datastore.FullEntity)1 ProjectionEntity (com.google.cloud.datastore.ProjectionEntity)1