Package com.google.cloud.spanner
Interface TransactionManager
- All Superinterfaces:
AutoCloseable
An interface for managing the life cycle of a read write transaction including all its retries.
See
TransactionContext
for a description of transaction semantics.
At any point in time there can be at most one active transaction in this manager. When that
transaction is committed, if it fails with an ABORTED
error, calling resetForRetry()
would create a new TransactionContext
. The newly created transaction
would use the same session thus increasing its lock priority. If the transaction is committed
successfully, or is rolled back or commit fails with any error other than ABORTED
, the
manager is considered complete and no further transactions are allowed to be created in it.
Every TransactionManager
should either be committed or rolled back. Failure to do so
can cause resources to be leaked and deadlocks. Easiest way to guarantee this is by calling
close()
in a finally block.
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic enum
State of the transaction manager. -
Method Summary
Modifier and TypeMethodDescriptionbegin()
Creates a new read write transaction.void
close()
Closes the manager.void
commit()
Commits the currently active transaction.Returns theCommitResponse
of this transaction.com.google.cloud.Timestamp
Returns the commit timestamp if the transaction committed successfully otherwise it will throwIllegalStateException
.getState()
Returns the state of the transaction.Creates a new transaction for retry.void
rollback()
Rolls back the currently active transaction.
-
Method Details
-
begin
TransactionContext begin()Creates a new read write transaction. This must be called before doing any other operation and can only be called once. To create a new transaction for subsequent retries, seeresetForRetry()
. -
commit
void commit()Commits the currently active transaction. If the transaction was already aborted, then this would throw anAbortedException
. -
rollback
void rollback()Rolls back the currently active transaction. In most cases there should be no need to call this explicitly sinceclose()
would automatically roll back any active transaction. -
resetForRetry
TransactionContext resetForRetry()Creates a new transaction for retry. This should only be called if the previous transaction failed withABORTED
. In all other cases, this will throw anIllegalStateException
. Users should backoff before calling this method. Backoff delay is specified bySpannerException.getRetryDelayInMillis()
on theSpannerException
throw by the previous commit call. -
getCommitTimestamp
com.google.cloud.Timestamp getCommitTimestamp()Returns the commit timestamp if the transaction committed successfully otherwise it will throwIllegalStateException
. -
getCommitResponse
CommitResponse getCommitResponse()Returns theCommitResponse
of this transaction. -
getState
TransactionManager.TransactionState getState()Returns the state of the transaction. -
close
void close()Closes the manager. If there is an active transaction, it will be rolled back. Underlying session will be released back to the session pool.- Specified by:
close
in interfaceAutoCloseable
-