Skip to content

Commit a1c50b5

Browse files
committed
docs: Add snippet for ReadLockMode configuration at client and transaction
1 parent 338a9b1 commit a1c50b5

File tree

3 files changed

+114
-49
lines changed

3 files changed

+114
-49
lines changed

samples/snippets/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<dependency>
3535
<groupId>com.google.cloud</groupId>
3636
<artifactId>libraries-bom</artifactId>
37-
<version>26.71.0</version>
37+
<version>26.73.0</version>
3838
<type>pom</type>
3939
<scope>import</scope>
4040
</dependency>
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2026 Google LLC
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.cloud.spanner.DatabaseClient;
20+
import com.google.cloud.spanner.DatabaseId;
21+
import com.google.cloud.spanner.Options;
22+
import com.google.cloud.spanner.ResultSet;
23+
import com.google.cloud.spanner.Spanner;
24+
import com.google.cloud.spanner.SpannerOptions;
25+
import com.google.cloud.spanner.SpannerOptions.Builder.DefaultReadWriteTransactionOptions;
26+
import com.google.cloud.spanner.Statement;
27+
import com.google.spanner.v1.TransactionOptions.IsolationLevel;
28+
import com.google.spanner.v1.TransactionOptions.ReadWrite.ReadLockMode;
29+
30+
public class IsolationLevelAndReadLockModeSample {
31+
32+
// [START spanner_isolation_level]
33+
static void isolationLevelSetting(DatabaseId db) {
34+
// The isolation level specified at the client-level will be applied to all
35+
// RW transactions.
36+
DefaultReadWriteTransactionOptions transactionOptions =
37+
DefaultReadWriteTransactionOptions.newBuilder()
38+
.setIsolationLevel(IsolationLevel.SERIALIZABLE)
39+
.build();
40+
SpannerOptions options =
41+
SpannerOptions.newBuilder()
42+
.setDefaultTransactionOptions(transactionOptions)
43+
.build();
44+
Spanner spanner = options.getService();
45+
DatabaseClient dbClient = spanner.getDatabaseClient(db);
46+
dbClient
47+
// The isolation level specified at the transaction-level takes precedence
48+
// over the isolation level configured at the client-level.
49+
.readWriteTransaction(Options.isolationLevel(IsolationLevel.REPEATABLE_READ))
50+
.run(transaction -> {
51+
// Read an AlbumTitle.
52+
String selectSql =
53+
"SELECT AlbumTitle from Albums WHERE SingerId = 1 and AlbumId = 1";
54+
ResultSet resultSet = transaction.executeQuery(Statement.of(selectSql));
55+
String title = null;
56+
while (resultSet.next()) {
57+
title = resultSet.getString("AlbumTitle");
58+
}
59+
System.out.printf("Current album title: %s\n", title);
60+
61+
// Update the title.
62+
String updateSql =
63+
"UPDATE Albums "
64+
+ "SET AlbumTitle = 'New Album Title' "
65+
+ "WHERE SingerId = 1 and AlbumId = 1";
66+
long rowCount = transaction.executeUpdate(Statement.of(updateSql));
67+
System.out.printf("%d record updated.\n", rowCount);
68+
return null;
69+
});
70+
}
71+
// [END spanner_isolation_level]
72+
73+
// [START spanner_read_lock_mode]
74+
static void readLockModeSetting(DatabaseId db) {
75+
// The read lock mode specified at the client-level will be applied to all
76+
// RW transactions.
77+
DefaultReadWriteTransactionOptions transactionOptions =
78+
DefaultReadWriteTransactionOptions.newBuilder()
79+
.setReadLockMode(ReadLockMode.OPTIMISTIC)
80+
.build();
81+
SpannerOptions options =
82+
SpannerOptions.newBuilder()
83+
.setDefaultTransactionOptions(transactionOptions)
84+
.build();
85+
Spanner spanner = options.getService();
86+
DatabaseClient dbClient = spanner.getDatabaseClient(db);
87+
dbClient
88+
// The read lock mode specified at the transaction-level takes precedence
89+
// over the read lock mode configured at the client-level.
90+
.readWriteTransaction(Options.readLockMode(ReadLockMode.PESSIMISTIC))
91+
.run(transaction -> {
92+
// Read an AlbumTitle.
93+
String selectSql =
94+
"SELECT AlbumTitle from Albums WHERE SingerId = 1 and AlbumId = 1";
95+
ResultSet resultSet = transaction.executeQuery(Statement.of(selectSql));
96+
String title = null;
97+
while (resultSet.next()) {
98+
title = resultSet.getString("AlbumTitle");
99+
}
100+
System.out.printf("Current album title: %s\n", title);
101+
102+
// Update the title.
103+
String updateSql =
104+
"UPDATE Albums "
105+
+ "SET AlbumTitle = 'New Album Title' "
106+
+ "WHERE SingerId = 1 and AlbumId = 1";
107+
long rowCount = transaction.executeUpdate(Statement.of(updateSql));
108+
System.out.printf("%d record updated.\n", rowCount);
109+
return null;
110+
});
111+
}
112+
// [END spanner_read_lock_mode]
113+
}

samples/snippets/src/main/java/com/example/spanner/SpannerSample.java

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,13 @@
3232
import com.google.cloud.spanner.KeyRange;
3333
import com.google.cloud.spanner.KeySet;
3434
import com.google.cloud.spanner.Mutation;
35-
import com.google.cloud.spanner.Options;
3635
import com.google.cloud.spanner.ReadOnlyTransaction;
3736
import com.google.cloud.spanner.ResultSet;
3837
import com.google.cloud.spanner.Spanner;
3938
import com.google.cloud.spanner.SpannerBatchUpdateException;
4039
import com.google.cloud.spanner.SpannerException;
4140
import com.google.cloud.spanner.SpannerExceptionFactory;
4241
import com.google.cloud.spanner.SpannerOptions;
43-
import com.google.cloud.spanner.SpannerOptions.Builder.DefaultReadWriteTransactionOptions;
4442
import com.google.cloud.spanner.Statement;
4543
import com.google.cloud.spanner.Struct;
4644
import com.google.cloud.spanner.TimestampBound;
@@ -73,7 +71,6 @@
7371
import com.google.spanner.admin.database.v1.RestoreDatabaseRequest;
7472
import com.google.spanner.admin.database.v1.RestoreInfo;
7573
import com.google.spanner.v1.ExecuteSqlRequest.QueryOptions;
76-
import com.google.spanner.v1.TransactionOptions.IsolationLevel;
7774
import java.math.BigDecimal;
7875
import java.time.Instant;
7976
import java.time.ZoneId;
@@ -1555,47 +1552,6 @@ static void queryWithQueryOptions(DatabaseClient dbClient) {
15551552
}
15561553
// [END spanner_query_with_query_options]
15571554

1558-
// [START spanner_isolation_level]
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]
1598-
15991555
// [START spanner_create_backup]
16001556
static void createBackup(DatabaseAdminClient dbAdminClient, String projectId, String instanceId,
16011557
String databaseId, String backupId, Timestamp versionTime) {
@@ -2152,9 +2108,6 @@ static void run(
21522108
case "querywithqueryoptions":
21532109
queryWithQueryOptions(dbClient);
21542110
break;
2155-
case "isolationlevelsettings":
2156-
isolationLevelSetting(database);
2157-
break;
21582111
case "createbackup":
21592112
createBackup(dbAdminClient, database.getInstanceId().getProject(),
21602113
database.getInstanceId().getInstance(), database.getDatabase(),
@@ -2265,7 +2218,6 @@ static void printUsageAndExit() {
22652218
System.err.println(" SpannerExample querywithtimestampparameter my-instance example-db");
22662219
System.err.println(" SpannerExample clientwithqueryoptions my-instance example-db");
22672220
System.err.println(" SpannerExample querywithqueryoptions my-instance example-db");
2268-
System.err.println(" SpannerExample isolationlevelsettings my-instance example-db");
22692221
System.err.println(" SpannerExample createbackup my-instance example-db");
22702222
System.err.println(" SpannerExample listbackups my-instance example-db");
22712223
System.err.println(" SpannerExample listbackupoperations my-instance example-db backup-id");

0 commit comments

Comments
 (0)