@@ -2540,6 +2540,32 @@ def read_then_write(transaction):
25402540 # [END spanner_transaction_timeout]
25412541
25422542
2543+ def set_statement_timeout (instance_id , database_id ):
2544+ """Executes a transaction with a statement timeout."""
2545+ # [START spanner_set_statement_timeout]
2546+ # instance_id = "your-spanner-instance"
2547+ # database_id = "your-spanner-db-id"
2548+ spanner_client = spanner .Client ()
2549+ instance = spanner_client .instance (instance_id )
2550+ database = instance .database (database_id )
2551+
2552+ def write (transaction ):
2553+ # Insert a record and configure the statement timeout to 60 seconds
2554+ # This timeout can however ONLY BE SHORTER than the default timeout
2555+ # for the RPC. If you set a timeout that is longer than the default timeout,
2556+ # then the default timeout will be used.
2557+ row_ct = transaction .execute_update (
2558+ "INSERT INTO Singers (SingerId, FirstName, LastName) "
2559+ " VALUES (110, 'George', 'Washington')" ,
2560+ timeout = 60 ,
2561+ )
2562+ print ("{} record(s) inserted." .format (row_ct ))
2563+
2564+ database .run_in_transaction (write )
2565+
2566+ # [END spanner_set_statement_timeout]
2567+
2568+
25432569def set_request_tag (instance_id , database_id ):
25442570 """Executes a snapshot read with a request tag."""
25452571 # [START spanner_set_request_tag]
@@ -3651,6 +3677,7 @@ def add_split_points(instance_id, database_id):
36513677 subparsers .add_parser (
36523678 "set_transaction_timeout" , help = set_transaction_timeout .__doc__
36533679 )
3680+ subparsers .add_parser ("set_statement_timeout" , help = set_statement_timeout .__doc__ )
36543681 subparsers .add_parser (
36553682 "query_data_with_new_column" , help = query_data_with_new_column .__doc__
36563683 )
@@ -3819,6 +3846,8 @@ def add_split_points(instance_id, database_id):
38193846 set_max_commit_delay (args .instance_id , args .database_id )
38203847 elif args .command == "set_transaction_timeout" :
38213848 set_transaction_timeout (args .instance_id , args .database_id )
3849+ elif args .command == "set_statement_timeout" :
3850+ set_statement_timeout (args .instance_id , args .database_id )
38223851 elif args .command == "query_data_with_new_column" :
38233852 query_data_with_new_column (args .instance_id , args .database_id )
38243853 elif args .command == "read_write_transaction" :
0 commit comments