|
32 | 32 | import com.google.cloud.spanner.KeyRange;
|
33 | 33 | import com.google.cloud.spanner.KeySet;
|
34 | 34 | import com.google.cloud.spanner.Mutation;
|
| 35 | +import com.google.cloud.spanner.Options; |
35 | 36 | import com.google.cloud.spanner.ReadOnlyTransaction;
|
36 | 37 | import com.google.cloud.spanner.ResultSet;
|
37 | 38 | import com.google.cloud.spanner.Spanner;
|
38 | 39 | import com.google.cloud.spanner.SpannerBatchUpdateException;
|
39 | 40 | import com.google.cloud.spanner.SpannerException;
|
40 | 41 | import com.google.cloud.spanner.SpannerExceptionFactory;
|
41 | 42 | import com.google.cloud.spanner.SpannerOptions;
|
| 43 | +import com.google.cloud.spanner.SpannerOptions.Builder.DefaultReadWriteTransactionOptions; |
42 | 44 | import com.google.cloud.spanner.Statement;
|
43 | 45 | import com.google.cloud.spanner.Struct;
|
44 | 46 | import com.google.cloud.spanner.TimestampBound;
|
|
71 | 73 | import com.google.spanner.admin.database.v1.RestoreDatabaseRequest;
|
72 | 74 | import com.google.spanner.admin.database.v1.RestoreInfo;
|
73 | 75 | import com.google.spanner.v1.ExecuteSqlRequest.QueryOptions;
|
| 76 | +import com.google.spanner.v1.TransactionOptions.IsolationLevel; |
74 | 77 | import java.math.BigDecimal;
|
75 | 78 | import java.time.Instant;
|
76 | 79 | import java.time.ZoneId;
|
@@ -1552,6 +1555,47 @@ static void queryWithQueryOptions(DatabaseClient dbClient) {
|
1552 | 1555 | }
|
1553 | 1556 | // [END spanner_query_with_query_options]
|
1554 | 1557 |
|
| 1558 | + // [START spanner_isolation_level_settings] |
| 1559 | + static void isolationLevelSetting(DatabaseId db) { |
| 1560 | + // The isolation level specified at the client-level will be applied to all |
| 1561 | + // RW transactions. |
| 1562 | + DefaultReadWriteTransactionOptions transactionOptions = |
| 1563 | + DefaultReadWriteTransactionOptions.newBuilder() |
| 1564 | + .setIsolationLevel(IsolationLevel.SERIALIZABLE) |
| 1565 | + .build(); |
| 1566 | + SpannerOptions options = |
| 1567 | + SpannerOptions.newBuilder() |
| 1568 | + .setDefaultTransactionOptions(transactionOptions) |
| 1569 | + .build(); |
| 1570 | + Spanner spanner = options.getService(); |
| 1571 | + DatabaseClient dbClient = spanner.getDatabaseClient(db); |
| 1572 | + dbClient |
| 1573 | + // The isolation level specified at the transaction-level takes precedence |
| 1574 | + // over the isolation level configured at the client-level. |
| 1575 | + .readWriteTransaction(Options.isolationLevel(IsolationLevel.REPEATABLE_READ)) |
| 1576 | + .run(transaction -> { |
| 1577 | + // Read an AlbumTitle. |
| 1578 | + String selectSql = |
| 1579 | + "SELECT AlbumTitle from Albums WHERE SingerId = 1 and AlbumId = 1"; |
| 1580 | + ResultSet resultSet = transaction.executeQuery(Statement.of(selectSql)); |
| 1581 | + String title = null; |
| 1582 | + while (resultSet.next()) { |
| 1583 | + title = resultSet.getString("AlbumTitle"); |
| 1584 | + } |
| 1585 | + System.out.printf("Current album title: %s\n", title); |
| 1586 | + |
| 1587 | + // Update the title. |
| 1588 | + String updateSql = |
| 1589 | + "UPDATE Albums " |
| 1590 | + + "SET AlbumTitle = 'New Album Title' " |
| 1591 | + + "WHERE SingerId = 1 and AlbumId = 1"; |
| 1592 | + long rowCount = transaction.executeUpdate(Statement.of(updateSql)); |
| 1593 | + System.out.printf("%d record updated.\n", rowCount); |
| 1594 | + return null; |
| 1595 | + }); |
| 1596 | + } |
| 1597 | + // [END spanner_isolation_level_settings] |
| 1598 | + |
1555 | 1599 | // [START spanner_create_backup]
|
1556 | 1600 | static void createBackup(DatabaseAdminClient dbAdminClient, String projectId, String instanceId,
|
1557 | 1601 | String databaseId, String backupId, Timestamp versionTime) {
|
@@ -2108,6 +2152,9 @@ static void run(
|
2108 | 2152 | case "querywithqueryoptions":
|
2109 | 2153 | queryWithQueryOptions(dbClient);
|
2110 | 2154 | break;
|
| 2155 | + case "isolationlevelsettings": |
| 2156 | + isolationLevelSetting(database); |
| 2157 | + break; |
2111 | 2158 | case "createbackup":
|
2112 | 2159 | createBackup(dbAdminClient, database.getInstanceId().getProject(),
|
2113 | 2160 | database.getInstanceId().getInstance(), database.getDatabase(),
|
@@ -2218,6 +2265,7 @@ static void printUsageAndExit() {
|
2218 | 2265 | System.err.println(" SpannerExample querywithtimestampparameter my-instance example-db");
|
2219 | 2266 | System.err.println(" SpannerExample clientwithqueryoptions my-instance example-db");
|
2220 | 2267 | System.err.println(" SpannerExample querywithqueryoptions my-instance example-db");
|
| 2268 | + System.err.println(" SpannerExample isolationlevelsettings my-instance example-db"); |
2221 | 2269 | System.err.println(" SpannerExample createbackup my-instance example-db");
|
2222 | 2270 | System.err.println(" SpannerExample listbackups my-instance example-db");
|
2223 | 2271 | System.err.println(" SpannerExample listbackupoperations my-instance example-db backup-id");
|
|
0 commit comments