| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,2 +1,2 @@ | |||
| 1 | - google-cloud-spanner==3.57.0 | ||
| 1 | + google-cloud-spanner==3.58.0 | ||
| 2 | 2 | futures==3.4.0; python_version < "3" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3186,14 +3186,13 @@ def isolation_level_options( | |||
| 3186 | 3186 | instance_id, | |
| 3187 | 3187 | database_id, | |
| 3188 | 3188 | ): | |
| 3189 | - from google.cloud.spanner_v1 import TransactionOptions, DefaultTransactionOptions | ||
| 3190 | - | ||
| 3191 | 3189 | """ | |
| 3192 | 3190 | Shows how to run a Read Write transaction with isolation level options. | |
| 3193 | 3191 | """ | |
| 3194 | 3192 | # [START spanner_isolation_level] | |
| 3195 | 3193 | # instance_id = "your-spanner-instance" | |
| 3196 | 3194 | # database_id = "your-spanner-db-id" | |
| 3195 | + from google.cloud.spanner_v1 import TransactionOptions, DefaultTransactionOptions | ||
| 3197 | 3196 | ||
| 3198 | 3197 | # The isolation level specified at the client-level will be applied to all RW transactions. | |
| 3199 | 3198 | isolation_options_for_client = TransactionOptions.IsolationLevel.SERIALIZABLE | |
@@ -3232,6 +3231,60 @@ def update_albums_with_isolation(transaction): | |||
| 3232 | 3231 | # [END spanner_isolation_level] | |
| 3233 | 3232 | ||
| 3234 | 3233 | ||
| 3234 | + def read_lock_mode_options( | ||
| 3235 | + instance_id, | ||
| 3236 | + database_id, | ||
| 3237 | + ): | ||
| 3238 | + """ | ||
| 3239 | + Shows how to run a Read Write transaction with read lock mode options. | ||
| 3240 | + """ | ||
| 3241 | + # [START spanner_read_lock_mode] | ||
| 3242 | + # instance_id = "your-spanner-instance" | ||
| 3243 | + # database_id = "your-spanner-db-id" | ||
| 3244 | + from google.cloud.spanner_v1 import TransactionOptions, DefaultTransactionOptions | ||
| 3245 | + | ||
| 3246 | + # The read lock mode specified at the client-level will be applied to all | ||
| 3247 | + # RW transactions. | ||
| 3248 | + read_lock_mode_options_for_client = TransactionOptions.ReadWrite.ReadLockMode.OPTIMISTIC | ||
| 3249 | + | ||
| 3250 | + # Create a client that uses Serializable isolation (default) with | ||
| 3251 | + # optimistic locking for read-write transactions. | ||
| 3252 | + spanner_client = spanner.Client( | ||
| 3253 | + default_transaction_options=DefaultTransactionOptions( | ||
| 3254 | + read_lock_mode=read_lock_mode_options_for_client | ||
| 3255 | + ) | ||
| 3256 | + ) | ||
| 3257 | + instance = spanner_client.instance(instance_id) | ||
| 3258 | + database = instance.database(database_id) | ||
| 3259 | + | ||
| 3260 | + # The read lock mode specified at the request level takes precedence over | ||
| 3261 | + # the read lock mode configured at the client level. | ||
| 3262 | + read_lock_mode_options_for_transaction = ( | ||
| 3263 | + TransactionOptions.ReadWrite.ReadLockMode.PESSIMISTIC | ||
| 3264 | + ) | ||
| 3265 | + | ||
| 3266 | + def update_albums_with_read_lock_mode(transaction): | ||
| 3267 | + # Read an AlbumTitle. | ||
| 3268 | + results = transaction.execute_sql( | ||
| 3269 | + "SELECT AlbumTitle from Albums WHERE SingerId = 2 and AlbumId = 1" | ||
| 3270 | + ) | ||
| 3271 | + for result in results: | ||
| 3272 | + print("Current Album Title: {}".format(*result)) | ||
| 3273 | + | ||
| 3274 | + # Update the AlbumTitle. | ||
| 3275 | + row_ct = transaction.execute_update( | ||
| 3276 | + "UPDATE Albums SET AlbumTitle = 'A New Title' WHERE SingerId = 2 and AlbumId = 1" | ||
| 3277 | + ) | ||
| 3278 | + | ||
| 3279 | + print("{} record(s) updated.".format(row_ct)) | ||
| 3280 | + | ||
| 3281 | + database.run_in_transaction( | ||
| 3282 | + update_albums_with_read_lock_mode, | ||
| 3283 | + read_lock_mode=read_lock_mode_options_for_transaction | ||
| 3284 | + ) | ||
| 3285 | + # [END spanner_read_lock_mode] | ||
| 3286 | + | ||
| 3287 | + | ||
| 3235 | 3288 | def set_custom_timeout_and_retry(instance_id, database_id): | |
| 3236 | 3289 | """Executes a snapshot read with custom timeout and retry.""" | |
| 3237 | 3290 | # [START spanner_set_custom_timeout_and_retry] | |
@@ -3856,6 +3909,9 @@ def add_split_points(instance_id, database_id): | |||
| 3856 | 3909 | subparsers.add_parser( | |
| 3857 | 3910 | "isolation_level_options", help=isolation_level_options.__doc__ | |
| 3858 | 3911 | ) | |
| 3912 | + subparsers.add_parser( | ||
| 3913 | + "read_lock_mode_options", help=read_lock_mode_options.__doc__ | ||
| 3914 | + ) | ||
| 3859 | 3915 | subparsers.add_parser( | |
| 3860 | 3916 | "set_custom_timeout_and_retry", help=set_custom_timeout_and_retry.__doc__ | |
| 3861 | 3917 | ) | |
@@ -4018,6 +4074,8 @@ def add_split_points(instance_id, database_id): | |||
| 4018 | 4074 | directed_read_options(args.instance_id, args.database_id) | |
| 4019 | 4075 | elif args.command == "isolation_level_options": | |
| 4020 | 4076 | isolation_level_options(args.instance_id, args.database_id) | |
| 4077 | + elif args.command == "read_lock_mode_options": | ||
| 4078 | + read_lock_mode_options(args.instance_id, args.database_id) | ||
| 4021 | 4079 | elif args.command == "set_custom_timeout_and_retry": | |
| 4022 | 4080 | set_custom_timeout_and_retry(args.instance_id, args.database_id) | |
| 4023 | 4081 | elif args.command == "create_instance_with_autoscaling_config": | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -993,12 +993,19 @@ def test_set_custom_timeout_and_retry(capsys, instance_id, sample_database): | |||
| 993 | 993 | ||
| 994 | 994 | ||
| 995 | 995 | @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): | ||
| 997 | 997 | snippets.isolation_level_options(instance_id, sample_database.database_id) | |
| 998 | 998 | out, _ = capsys.readouterr() | |
| 999 | 999 | assert "1 record(s) updated." in out | |
| 1000 | 1000 | ||
| 1001 | 1001 | ||
| 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 | + | ||
| 1002 | 1009 | @pytest.mark.dependency( | |
| 1003 | 1010 | name="add_proto_types_column", | |
| 1004 | 1011 | ) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments