Search in sources :

Example 6 with TransactionSelector

use of com.google.spanner.v1.TransactionSelector in project java-spanner by googleapis.

the class PartitionedDmlTransaction method newTransactionRequestFrom.

@VisibleForTesting
ExecuteSqlRequest newTransactionRequestFrom(final Statement statement, final Options options) {
    ByteString transactionId = initTransaction();
    final TransactionSelector transactionSelector = TransactionSelector.newBuilder().setId(transactionId).build();
    final ExecuteSqlRequest.Builder builder = ExecuteSqlRequest.newBuilder().setSql(statement.getSql()).setQueryMode(ExecuteSqlRequest.QueryMode.NORMAL).setSession(session.getName()).setTransaction(transactionSelector);
    setParameters(builder, statement.getParameters());
    builder.setResumeToken(ByteString.EMPTY);
    if (options.hasPriority() || options.hasTag()) {
        RequestOptions.Builder requestOptionsBuilder = RequestOptions.newBuilder();
        if (options.hasPriority()) {
            requestOptionsBuilder.setPriority(options.priority());
        }
        if (options.hasTag()) {
            requestOptionsBuilder.setRequestTag(options.tag());
        }
        builder.setRequestOptions(requestOptionsBuilder.build());
    }
    return builder.build();
}
Also used : ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) RequestOptions(com.google.spanner.v1.RequestOptions) ByteString(com.google.protobuf.ByteString) TransactionSelector(com.google.spanner.v1.TransactionSelector) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 7 with TransactionSelector

use of com.google.spanner.v1.TransactionSelector 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)

Example 8 with TransactionSelector

use of com.google.spanner.v1.TransactionSelector 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 9 with TransactionSelector

use of com.google.spanner.v1.TransactionSelector in project java-spanner by googleapis.

the class AbstractReadContext method getExecuteSqlRequestBuilder.

ExecuteSqlRequest.Builder getExecuteSqlRequestBuilder(Statement statement, QueryMode queryMode, Options options, boolean withTransactionSelector) {
    ExecuteSqlRequest.Builder builder = ExecuteSqlRequest.newBuilder().setSql(statement.getSql()).setQueryMode(queryMode).setSession(session.getName());
    Map<String, Value> stmtParameters = statement.getParameters();
    if (!stmtParameters.isEmpty()) {
        com.google.protobuf.Struct.Builder paramsBuilder = builder.getParamsBuilder();
        for (Map.Entry<String, Value> param : stmtParameters.entrySet()) {
            paramsBuilder.putFields(param.getKey(), Value.toProto(param.getValue()));
            if (param.getValue() != null) {
                builder.putParamTypes(param.getKey(), param.getValue().getType().toProto());
            }
        }
    }
    if (withTransactionSelector) {
        TransactionSelector selector = getTransactionSelector();
        if (selector != null) {
            builder.setTransaction(selector);
        }
    }
    builder.setSeqno(getSeqNo());
    builder.setQueryOptions(buildQueryOptions(statement.getQueryOptions()));
    builder.setRequestOptions(buildRequestOptions(options));
    return builder;
}
Also used : ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) TransactionSelector(com.google.spanner.v1.TransactionSelector) ByteString(com.google.protobuf.ByteString) Map(java.util.Map)

Example 10 with TransactionSelector

use of com.google.spanner.v1.TransactionSelector in project java-spanner by googleapis.

the class AsyncTransactionManagerTest method asyncTransactionManager_shouldRollbackOnCloseAsync.

@Test
public void asyncTransactionManager_shouldRollbackOnCloseAsync() throws Exception {
    AsyncTransactionManager manager = client().transactionManagerAsync();
    TransactionContext txn = manager.beginAsync().get();
    txn.executeUpdateAsync(UPDATE_STATEMENT).get();
    final TransactionSelector selector = ((TransactionContextImpl) ((SessionPoolTransactionContext) txn).delegate).getTransactionSelector();
    SpannerApiFutures.get(manager.closeAsync());
    // The mock server should already have the Rollback request, as we are waiting for the returned
    // ApiFuture to be done.
    mockSpanner.waitForRequestsToContain(input -> {
        if (input instanceof RollbackRequest) {
            RollbackRequest request = (RollbackRequest) input;
            return request.getTransactionId().equals(selector.getId());
        }
        return false;
    }, 0L);
}
Also used : TransactionContextImpl(com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl) SessionPoolTransactionContext(com.google.cloud.spanner.SessionPool.SessionPoolTransactionContext) TransactionSelector(com.google.spanner.v1.TransactionSelector) RollbackRequest(com.google.spanner.v1.RollbackRequest) Test(org.junit.Test)

Aggregations

TransactionSelector (com.google.spanner.v1.TransactionSelector)13 ByteString (com.google.protobuf.ByteString)7 TransactionOptions (com.google.spanner.v1.TransactionOptions)6 Session (com.google.spanner.v1.Session)5 PartitionResponse (com.google.spanner.v1.PartitionResponse)4 Test (org.junit.Test)4 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)3 PartitionQueryRequest (com.google.spanner.v1.PartitionQueryRequest)3 Transaction (com.google.spanner.v1.Transaction)3 GrpcResultSet (com.google.cloud.spanner.AbstractResultSet.GrpcResultSet)2 GrpcStreamIterator (com.google.cloud.spanner.AbstractResultSet.GrpcStreamIterator)2 ResumableStreamIterator (com.google.cloud.spanner.AbstractResultSet.ResumableStreamIterator)2 SpannerRpc (com.google.cloud.spanner.spi.v1.SpannerRpc)2 GcpManagedChannel (com.google.grpc.gcp.GcpManagedChannel)2 PartialResultSet (com.google.spanner.v1.PartialResultSet)2 ResultSetMetadata (com.google.spanner.v1.ResultSetMetadata)2 SpannerStub (com.google.spanner.v1.SpannerGrpc.SpannerStub)2 ManagedChannel (io.grpc.ManagedChannel)2 Map (java.util.Map)2 Nullable (javax.annotation.Nullable)2