This page describes how to set a timeout for transactions by using the
Spanner client libraries. The transaction fails with a
DEADLINE_EXCEEDED error if the transaction can't finish within the given
timeout value.
If the timeout error occurs during the execution of the
Commit request, it's still possible that the transaction was
committed.
RPC timeouts and transaction retry loops
RPC timeouts and transaction timeouts limit different scopes of work:
- An RPC timeout limits how long a single request, such as a query, a read, or
a
Commitrequest, can run. A transaction timeout that is longer than an RPC timeout doesn't extend the RPC timeout. For more information, see Configure custom timeouts and retries. - A transaction timeout limits the total time that the client library spends running a read-write transaction, including every attempt that the client library makes after Spanner aborts the transaction.
When Spanner aborts a read-write transaction, the client library reruns the transaction function. Short RPC timeouts don't limit how long the client library keeps retrying an aborted transaction. If you don't set a transaction timeout, the default retry behavior depends on the client library:
- Java: The transaction runner retries aborted transactions for up to 24 hours, with no limit on the number of attempts.
- Go: The client library retries aborted transactions until the transaction
commits, or until the
context.Contextobject that you pass to the transaction reaches its deadline or is canceled. - Node.js: The client library stops retrying aborted transactions after
1 hour (
3600000milliseconds). To change this limit, set thetimeoutfield in theRunTransactionOptionsobject that you pass to therunTransactionorrunTransactionAsyncmethod. - Python: The client library stops retrying aborted transactions after
30 seconds. To change this limit, pass the
timeout_secsargument to therun_in_transactionmethod.
In workloads with high lock contention, every retry attempt competes for the same locks, so a long retry loop can increase lock wait times for other transactions. To limit the total time that a transaction can run, set a transaction timeout as shown in the samples on this page.
Set a transaction timeout
You can set a transaction timeout by using the Go, Java, Python, and Node.js client libraries.
Go
Java
Node.js
Python
Set a timeout for asynchronous Java transactions
The Java sample on this page uses a gRPC Context object with a deadline to set
the transaction timeout. The gRPC Context object is attached to the current
thread.
When you run a transaction asynchronously by using the AsyncRunner.runAsync()
method, the client library runs the transaction on the Executor object that
you provide. The client library doesn't propagate the calling thread's Context
object, including its deadline, to that executor. As a result, the transaction
runs without the timeout and can keep retrying aborted attempts for up to
24 hours.
To apply the timeout to an asynchronous transaction, do one of the following:
- Wrap the executor by calling the
Context.currentContextExecutor(executor)method while theContextobject that has the deadline is current, and pass the wrapped executor to therunAsync()method. - Use the
AsyncTransactionManagerorTransactionManagerinterface instead of theAsyncRunnerinterface. With a transaction manager, you write the retry loop yourself and can stop retrying when your timeout expires.