@@ -3186,11 +3186,11 @@ def isolation_level_options(
31863186 instance_id ,
31873187 database_id ,
31883188):
3189- from google .cloud .spanner_v1 import TransactionOptions , DefaultTransactionOptions
3190-
31913189 """
31923190 Shows how to run a Read Write transaction with isolation level options.
31933191 """
3192+ from google .cloud .spanner_v1 import TransactionOptions , DefaultTransactionOptions
3193+
31943194 # [START spanner_isolation_level]
31953195 # instance_id = "your-spanner-instance"
31963196 # database_id = "your-spanner-db-id"
@@ -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+ """
3240+ Shows how to run a Read Write transaction with read lock mode options.
3241+ """
3242+ from google .cloud .spanner_v1 import TransactionOptions , DefaultTransactionOptions
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+
32353290def 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" :
0 commit comments