|
| 1 | +/* |
| 2 | + * Copyright 2023 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 | +// [START spanner_transaction_timeout] |
| 20 | + |
| 21 | +import com.google.cloud.spanner.DatabaseClient; |
| 22 | +import com.google.cloud.spanner.DatabaseId; |
| 23 | +import com.google.cloud.spanner.ResultSet; |
| 24 | +import com.google.cloud.spanner.Spanner; |
| 25 | +import com.google.cloud.spanner.SpannerOptions; |
| 26 | +import com.google.cloud.spanner.Statement; |
| 27 | +import io.grpc.Context; |
| 28 | +import io.grpc.Context.CancellableContext; |
| 29 | +import io.grpc.Deadline; |
| 30 | +import java.util.concurrent.Executors; |
| 31 | +import java.util.concurrent.ScheduledExecutorService; |
| 32 | +import java.util.concurrent.TimeUnit; |
| 33 | + |
| 34 | +/** |
| 35 | + * Sample showing how to set a timeout for an entire transaction for the Cloud Spanner Java client. |
| 36 | + */ |
| 37 | +class TransactionTimeoutExample { |
| 38 | + |
| 39 | + static void executeTransactionWithTimeout() { |
| 40 | + // TODO(developer): Replace these variables before running the sample. |
| 41 | + String projectId = "my-project"; |
| 42 | + String instanceId = "my-instance"; |
| 43 | + String databaseId = "my-database"; |
| 44 | + |
| 45 | + executeTransactionWithTimeout(projectId, instanceId, databaseId, 60L, TimeUnit.SECONDS); |
| 46 | + } |
| 47 | + |
| 48 | + // Execute a read/write transaction with a timeout for the entire transaction. |
| 49 | + static void executeTransactionWithTimeout( |
| 50 | + String projectId, |
| 51 | + String instanceId, |
| 52 | + String databaseId, |
| 53 | + long timeoutValue, |
| 54 | + TimeUnit timeoutUnit) { |
| 55 | + try (Spanner spanner = SpannerOptions.newBuilder().setProjectId(projectId).build() |
| 56 | + .getService()) { |
| 57 | + DatabaseClient client = |
| 58 | + spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId)); |
| 59 | + // Create a gRPC context with a deadline and with cancellation. |
| 60 | + // gRPC context deadlines require the use of a scheduled executor. |
| 61 | + ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); |
| 62 | + try (CancellableContext context = |
| 63 | + Context.current() |
| 64 | + .withDeadline(Deadline.after(timeoutValue, timeoutUnit), executor) |
| 65 | + .withCancellation()) { |
| 66 | + context.run( |
| 67 | + () -> { |
| 68 | + client |
| 69 | + .readWriteTransaction() |
| 70 | + .run( |
| 71 | + transaction -> { |
| 72 | + try (ResultSet resultSet = |
| 73 | + transaction.executeQuery( |
| 74 | + Statement.of( |
| 75 | + "SELECT SingerId, FirstName, LastName\n" |
| 76 | + + "FROM Singers\n" |
| 77 | + + "ORDER BY LastName, FirstName"))) { |
| 78 | + while (resultSet.next()) { |
| 79 | + System.out.printf( |
| 80 | + "%d %s %s\n", |
| 81 | + resultSet.getLong("SingerId"), |
| 82 | + resultSet.getString("FirstName"), |
| 83 | + resultSet.getString("LastName")); |
| 84 | + } |
| 85 | + } |
| 86 | + String sql = |
| 87 | + "INSERT INTO Singers (SingerId, FirstName, LastName)\n" |
| 88 | + + "VALUES (20, 'George', 'Washington')"; |
| 89 | + long rowCount = transaction.executeUpdate(Statement.of(sql)); |
| 90 | + System.out.printf("%d record inserted.%n", rowCount); |
| 91 | + return null; |
| 92 | + }); |
| 93 | + }); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | +// [END spanner_transaction_timeout] |
0 commit comments