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+ }
0 commit comments