|
| 1 | +/* |
| 2 | + * Copyright 2020 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.spanner; |
| 18 | + |
| 19 | +import com.google.api.gax.grpc.GrpcCallContext; |
| 20 | +import com.google.api.gax.rpc.ApiCallContext; |
| 21 | +import com.google.cloud.spanner.DatabaseClient; |
| 22 | +import com.google.cloud.spanner.DatabaseId; |
| 23 | +import com.google.cloud.spanner.Spanner; |
| 24 | +import com.google.cloud.spanner.SpannerOptions; |
| 25 | +import com.google.cloud.spanner.SpannerOptions.CallContextConfigurator; |
| 26 | +import com.google.cloud.spanner.Statement; |
| 27 | +import com.google.cloud.spanner.TransactionContext; |
| 28 | +import com.google.cloud.spanner.TransactionRunner.TransactionCallable; |
| 29 | +import com.google.spanner.v1.SpannerGrpc; |
| 30 | +import io.grpc.CallOptions; |
| 31 | +import io.grpc.Context; |
| 32 | +import io.grpc.MethodDescriptor; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | + |
| 35 | +class StatementTimeoutExample { |
| 36 | + |
| 37 | + static void executeSqlWithTimeout() { |
| 38 | + // TODO(developer): Replace these variables before running the sample. |
| 39 | + String projectId = "my-project"; |
| 40 | + String instanceId = "my-instance"; |
| 41 | + String databaseId = "my-database"; |
| 42 | + |
| 43 | + try (Spanner spanner = |
| 44 | + SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { |
| 45 | + DatabaseClient client = |
| 46 | + spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId)); |
| 47 | + executeSqlWithTimeout(client); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + static void executeSqlWithTimeout(DatabaseClient client) { |
| 52 | + CallContextConfigurator configurator = new CallContextConfigurator() { |
| 53 | + public <ReqT, RespT> ApiCallContext configure(ApiCallContext context, ReqT request, |
| 54 | + MethodDescriptor<ReqT, RespT> method) { |
| 55 | + // DML uses the ExecuteSql RPC. |
| 56 | + if (method == SpannerGrpc.getExecuteSqlMethod()) { |
| 57 | + return GrpcCallContext.createDefault() |
| 58 | + .withCallOptions(CallOptions.DEFAULT.withDeadlineAfter(60L, TimeUnit.SECONDS)); |
| 59 | + } |
| 60 | + // Return null to indicate that the default should be used for other methods. |
| 61 | + return null; |
| 62 | + } |
| 63 | + }; |
| 64 | + // Create a context that uses the custom call configuration. |
| 65 | + Context context = |
| 66 | + Context.current().withValue(SpannerOptions.CALL_CONTEXT_CONFIGURATOR_KEY, configurator); |
| 67 | + // Run the transaction in the custom context. |
| 68 | + context.run(new Runnable() { |
| 69 | + public void run() { |
| 70 | + client.readWriteTransaction().run(new TransactionCallable<long[]>() { |
| 71 | + public long[] run(TransactionContext transaction) throws Exception { |
| 72 | + String sql = "INSERT Singers (SingerId, FirstName, LastName)\n" |
| 73 | + + "VALUES (20, 'George', 'Washington')"; |
| 74 | + long rowCount = transaction.executeUpdate(Statement.of(sql)); |
| 75 | + System.out.printf("%d record inserted.%n", rowCount); |
| 76 | + return null; |
| 77 | + } |
| 78 | + }); |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | +} |
0 commit comments