Search code examples
google-cloud-platformgoogle-cloud-spanner

Way to prevent transaction timeout?


I'm running read-write transactions that are taking longer than 10 seconds, and they are timing out (failing with ABORTED errors). Is there a way to specify a longer timeout?


Solution

  • There is no way to specify the timeout for a transaction, but you have a few options:

    1. You could periodically issue an executeSql request every 5-8 seconds to keep your transaction alive. You can do a trivial query like SELECT 1. More info on idle transactions is here.
    2. You could use a read-only transaction instead of a read-write transaction. Read-only transactions do not have a timeout, and they are only aborted if the underlying session is deleted. (Sessions can be deleted manually, or automatically after approximately an hour of idle time.)

    You should consider whether you really need such a long read-write transaction. Read-write transactions use locks and other resources that can block other transactions from making progress. This is usually an anti-pattern to have such long-lived read-write transactions.