Skip to content

Commit 69a5380

Browse files
committed
updated the sample
1 parent 1551e28 commit 69a5380

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

samples/samples/snippets.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3188,27 +3188,34 @@ def isolation_level_options(
31883188
# instance_id = "your-spanner-instance"
31893189
# database_id = "your-spanner-db-id"
31903190

3191-
isolation_level_options_for_client = TransactionOptions.IsolationLevel.SERIALIZABLE
3192-
3193-
# The isolation level specified at the client level via default_transaction_options will be applied to all RW transactions
3191+
# The isolation level specified at the client-level will be applied to all RW transactions.
3192+
isolation_options_for_client = TransactionOptions.IsolationLevel.SERIALIZABLE
3193+
31943194
spanner_client = spanner.Client(
3195-
default_transaction_options=DefaultTransactionOptions(isolation_level=isolation_level_options_for_client)
3195+
default_transaction_options=DefaultTransactionOptions(isolation_level=isolation_options_for_client)
31963196
)
31973197
instance = spanner_client.instance(instance_id)
31983198
database = instance.database(database_id)
31993199

3200-
isolation_level_options_for_request = TransactionOptions.IsolationLevel.REPEATABLE_READ
3200+
# The isolation level specified at the request level takes precedence over the isolation level configured at the client level.
3201+
isolation_options_for_transaction = TransactionOptions.IsolationLevel.REPEATABLE_READ
32013202

3202-
def insert_singers(transaction):
3203+
def update_albums_with_isolation(transaction):
3204+
# Read an AlbumTitle.
3205+
results = transaction.execute_sql(
3206+
"SELECT AlbumTitle from Albums WHERE SingerId = 1 and AlbumId = 1"
3207+
)
3208+
for result in results:
3209+
print("Current Album Title: {}".format(*result))
3210+
3211+
# Update the AlbumTitle.
32033212
row_ct = transaction.execute_update(
3204-
"INSERT INTO Singers (SingerId, FirstName, LastName) "
3205-
" VALUES (20, 'Virginia', 'Watson')"
3213+
"UPDATE Albums SET AlbumTitle = 'A New Title' WHERE SingerId = 1 and AlbumId = 1"
32063214
)
32073215

3208-
print("{} record(s) inserted.".format(row_ct))
3216+
print("{} record(s) updated.".format(row_ct))
32093217

3210-
# The isolation level specified at the request level takes precedence over the isolation level configured at the client level.
3211-
database.run_in_transaction(insert_singers, isolation_level=isolation_level_options_for_request)
3218+
database.run_in_transaction(update_albums_with_isolation, isolation_level=isolation_options_for_transaction)
32123219
# [END spanner_isolation_level]
32133220

32143221

samples/samples/snippets_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ def test_directed_read_options(capsys, instance_id, sample_database):
986986
def test_isolated_level_options(capsys, instance_id, sample_database):
987987
snippets.isolation_level_options(instance_id, sample_database.database_id)
988988
out, _ = capsys.readouterr()
989-
assert "1 record(s) inserted." in out
989+
assert "1 record(s) updated." in out
990990

991991
@pytest.mark.dependency(depends=["insert_data"])
992992
def test_set_custom_timeout_and_retry(capsys, instance_id, sample_database):

0 commit comments

Comments
 (0)