chore: add samples for transaction timeout configuration (#1380) · googleapis/python-spanner@aa4880b · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit aa4880b

Browse files
authored
chore: add samples for transaction timeout configuration (#1380)
1 parent b433281 commit aa4880b

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

samples/samples/snippets.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,6 +2510,36 @@ def update_venues(transaction):
25102510
# [END spanner_set_transaction_tag]
25112511

25122512

2513+
def set_transaction_timeout(instance_id, database_id):
2514+
"""Executes a transaction with a transaction timeout."""
2515+
# [START spanner_transaction_timeout]
2516+
# instance_id = "your-spanner-instance"
2517+
# database_id = "your-spanner-db-id"
2518+
spanner_client = spanner.Client()
2519+
instance = spanner_client.instance(instance_id)
2520+
database = instance.database(database_id)
2521+
2522+
def read_then_write(transaction):
2523+
# Read records.
2524+
results = transaction.execute_sql(
2525+
"SELECT SingerId, FirstName, LastName FROM Singers ORDER BY LastName, FirstName"
2526+
)
2527+
for result in results:
2528+
print("SingerId: {}, FirstName: {}, LastName: {}".format(*result))
2529+
2530+
# Insert a record.
2531+
row_ct = transaction.execute_update(
2532+
"INSERT INTO Singers (SingerId, FirstName, LastName) "
2533+
" VALUES (100, 'George', 'Washington')"
2534+
)
2535+
print("{} record(s) inserted.".format(row_ct))
2536+
2537+
# configure transaction timeout to 60 seconds
2538+
database.run_in_transaction(read_then_write, timeout_secs=60)
2539+
2540+
# [END spanner_transaction_timeout]
2541+
2542+
25132543
def set_request_tag(instance_id, database_id):
25142544
"""Executes a snapshot read with a request tag."""
25152545
# [START spanner_set_request_tag]
@@ -3272,6 +3302,7 @@ def update_instance_default_backup_schedule_type(instance_id):
32723302

32733303
print("Updated instance {} to have default backup schedules".format(instance_id))
32743304

3305+
32753306
# [END spanner_update_instance_default_backup_schedule_type]
32763307

32773308

@@ -3617,6 +3648,9 @@ def add_split_points(instance_id, database_id):
36173648
subparsers.add_parser("add_column", help=add_column.__doc__)
36183649
subparsers.add_parser("update_data", help=update_data.__doc__)
36193650
subparsers.add_parser("set_max_commit_delay", help=set_max_commit_delay.__doc__)
3651+
subparsers.add_parser(
3652+
"set_transaction_timeout", help=set_transaction_timeout.__doc__
3653+
)
36203654
subparsers.add_parser(
36213655
"query_data_with_new_column", help=query_data_with_new_column.__doc__
36223656
)
@@ -3783,6 +3817,8 @@ def add_split_points(instance_id, database_id):
37833817
update_data(args.instance_id, args.database_id)
37843818
elif args.command == "set_max_commit_delay":
37853819
set_max_commit_delay(args.instance_id, args.database_id)
3820+
elif args.command == "set_transaction_timeout":
3821+
set_transaction_timeout(args.instance_id, args.database_id)
37863822
elif args.command == "query_data_with_new_column":
37873823
query_data_with_new_column(args.instance_id, args.database_id)
37883824
elif args.command == "read_write_transaction":

samples/samples/snippets_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,13 @@ def test_set_transaction_tag(capsys, instance_id, sample_database):
855855
assert "New venue inserted." in out
856856

857857

858+
@pytest.mark.dependency(depends=["insert_datatypes_data"])
859+
def test_set_transaction_timeout(capsys, instance_id, sample_database):
860+
snippets.set_transaction_timeout(instance_id, sample_database.database_id)
861+
out, _ = capsys.readouterr()
862+
assert "1 record(s) inserted." in out
863+
864+
858865
@pytest.mark.dependency(depends=["insert_data"])
859866
def test_set_request_tag(capsys, instance_id, sample_database):
860867
snippets.set_request_tag(instance_id, sample_database.database_id)

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.