Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit 7ddc6db

Browse files
committed
docs(spanner): snippet for setting read lock mode
Snippet shows how to set the read lock mode at the client-level and how to override the option at the transaction-level.
1 parent 3b1792a commit 7ddc6db

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

samples/samples/snippets.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3232,6 +3232,61 @@ def update_albums_with_isolation(transaction):
32323232
# [END spanner_isolation_level]
32333233

32343234

3235+
def read_lock_mode_options(
3236+
instance_id,
3237+
database_id,
3238+
):
3239+
from google.cloud.spanner_v1 import TransactionOptions, DefaultTransactionOptions
3240+
3241+
"""
3242+
Shows how to run a Read Write transaction with read lock mode options.
3243+
"""
3244+
# [START spanner_read_lock_mode]
3245+
# instance_id = "your-spanner-instance"
3246+
# database_id = "your-spanner-db-id"
3247+
3248+
# The read lock mode specified at the client-level will be applied to all
3249+
# RW transactions.
3250+
read_lock_mode_options_for_client = TransactionOptions.ReadWrite.ReadLockMode.OPTIMISTIC
3251+
3252+
# Create a client that uses Serializable isolation (default) with
3253+
# optimistic locking for read-write transactions.
3254+
spanner_client = spanner.Client(
3255+
default_transaction_options=DefaultTransactionOptions(
3256+
read_lock_mode=read_lock_mode_options_for_client
3257+
)
3258+
)
3259+
instance = spanner_client.instance(instance_id)
3260+
database = instance.database(database_id)
3261+
3262+
# The read lock mode specified at the request level takes precedence over
3263+
# the read lock mode configured at the client level.
3264+
read_lock_mode_options_for_transaction = (
3265+
TransactionOptions.ReadWrite.ReadLockMode.PESSIMISTIC
3266+
)
3267+
3268+
def update_albums_with_read_lock_mode(transaction):
3269+
# Read an AlbumTitle.
3270+
results = transaction.execute_sql(
3271+
"SELECT AlbumTitle from Albums WHERE SingerId = 2 and AlbumId = 1"
3272+
)
3273+
for result in results:
3274+
print("Current Album Title: {}".format(*result))
3275+
3276+
# Update the AlbumTitle.
3277+
row_ct = transaction.execute_update(
3278+
"UPDATE Albums SET AlbumTitle = 'A New Title' WHERE SingerId = 2 and AlbumId = 1"
3279+
)
3280+
3281+
print("{} record(s) updated.".format(row_ct))
3282+
3283+
database.run_in_transaction(
3284+
update_albums_with_read_lock_mode,
3285+
read_lock_mode=read_lock_mode_options_for_transaction
3286+
)
3287+
# [END spanner_read_lock_mode]
3288+
3289+
32353290
def set_custom_timeout_and_retry(instance_id, database_id):
32363291
"""Executes a snapshot read with custom timeout and retry."""
32373292
# [START spanner_set_custom_timeout_and_retry]
@@ -3856,6 +3911,9 @@ def add_split_points(instance_id, database_id):
38563911
subparsers.add_parser(
38573912
"isolation_level_options", help=isolation_level_options.__doc__
38583913
)
3914+
subparsers.add_parser(
3915+
"read_lock_mode_options", help=read_lock_mode_options.__doc__
3916+
)
38593917
subparsers.add_parser(
38603918
"set_custom_timeout_and_retry", help=set_custom_timeout_and_retry.__doc__
38613919
)
@@ -4018,6 +4076,8 @@ def add_split_points(instance_id, database_id):
40184076
directed_read_options(args.instance_id, args.database_id)
40194077
elif args.command == "isolation_level_options":
40204078
isolation_level_options(args.instance_id, args.database_id)
4079+
elif args.command == "read_lock_mode_options":
4080+
read_lock_mode_options(args.instance_id, args.database_id)
40214081
elif args.command == "set_custom_timeout_and_retry":
40224082
set_custom_timeout_and_retry(args.instance_id, args.database_id)
40234083
elif args.command == "create_instance_with_autoscaling_config":

samples/samples/snippets_test.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,12 +993,19 @@ def test_set_custom_timeout_and_retry(capsys, instance_id, sample_database):
993993

994994

995995
@pytest.mark.dependency(depends=["insert_data"])
996-
def test_isolated_level_options(capsys, instance_id, sample_database):
996+
def test_isolation_level_options(capsys, instance_id, sample_database):
997997
snippets.isolation_level_options(instance_id, sample_database.database_id)
998998
out, _ = capsys.readouterr()
999999
assert "1 record(s) updated." in out
10001000

10011001

1002+
@pytest.mark.dependency(depends=["insert_data"])
1003+
def test_read_lock_mode_options(capsys, instance_id, sample_database):
1004+
snippets.read_lock_mode_options(instance_id, sample_database.database_id)
1005+
out, _ = capsys.readouterr()
1006+
assert "1 record(s) updated." in out
1007+
1008+
10021009
@pytest.mark.dependency(
10031010
name="add_proto_types_column",
10041011
)

0 commit comments

Comments
 (0)