Class LocalTransactionHandler
- All Implemented Interfaces:
TransactionHandler
TransactionHandler uses local JDBC transactions
demarcated explicitly on the handle and passed through to be handled
directly on the JDBC Connection instance.
A connection that arrives with autocommit disabled is treated as enrolled
in a transaction that the connection owner manages. The handle reports an
open transaction, inTransaction and useTransaction join
that transaction without a commit, begin does nothing, and explicit
commit and rollback operate on the open transaction and can
repeat. Closing such a handle does not commit, roll back, or throw. See the
"Transactions managed outside Jdbi" section of the User Guide.
Use managed() for a handler that manages transactions itself on
connections with autocommit disabled instead of joining them.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidBegin a transaction.static LocalTransactionHandlerbinding()voidCommit the transaction.<R, X extends Exception>
RinTransaction(Handle handle, HandleCallback<R, X> callback) Run a transaction.<R, X extends Exception>
RinTransaction(Handle handle, TransactionIsolationLevel level, HandleCallback<R, X> callback) Run a transaction.booleanisInTransaction(Handle handle) Returns whether a given Handle is in the transaction.static LocalTransactionHandlermanaged()Returns a handler that manages transactions itself on every connection, including a connection with autocommit disabled.voidreleaseSavepoint(Handle handle, String savepointName) Release a previously created savepoint.voidvoidRoll back the transaction.voidrollbackToSavepoint(Handle handle, String savepointName) Roll back to a named savepoint.voidCreate a new savepoint.Methods inherited from class Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface TransactionHandler
specialize
-
Constructor Details
-
LocalTransactionHandler
public LocalTransactionHandler()
-
-
Method Details
-
begin
Description copied from interface:TransactionHandlerBegin a transaction.- Specified by:
beginin interfaceTransactionHandler- Parameters:
handle- the handle the transaction is being started on
-
commit
Description copied from interface:TransactionHandlerCommit the transaction.- Specified by:
commitin interfaceTransactionHandler- Parameters:
handle- the handle the commit is being performed on
-
rollback
Description copied from interface:TransactionHandlerRoll back the transaction.- Specified by:
rollbackin interfaceTransactionHandler- Parameters:
handle- the handle the rollback is being performed on
-
isInTransaction
Description copied from interface:TransactionHandlerReturns whether a given Handle is in the transaction.- Specified by:
isInTransactionin interfaceTransactionHandler- Parameters:
handle- the handle to test- Returns:
- whether the given handle is in a transaction
-
savepoint
Description copied from interface:TransactionHandlerCreate a new savepoint.- Specified by:
savepointin interfaceTransactionHandler- Parameters:
handle- the handle on which the transaction is being savepointedsavepointName- The name of the savepoint, used to rollback to or release later
-
rollbackToSavepoint
Description copied from interface:TransactionHandlerRoll back to a named savepoint.- Specified by:
rollbackToSavepointin interfaceTransactionHandler- Parameters:
handle- the handle the rollback is being performed onsavepointName- the name of the savepoint to rollback to
-
releaseSavepoint
Description copied from interface:TransactionHandlerRelease a previously created savepoint.- Specified by:
releaseSavepointin interfaceTransactionHandler- Parameters:
handle- the handle on which the savepoint is being releasedsavepointName- the savepoint to release
-
inTransaction
public <R, X extends Exception> R inTransaction(Handle handle, HandleCallback<R, X> callback) throws XDescription copied from interface:TransactionHandlerRun a transaction.- Specified by:
inTransactionin interfaceTransactionHandler- Type Parameters:
R- the callback return typeX- the exception type thrown by the callback, if any- Parameters:
handle- the handle to the databasecallback- a callback which will receive the open handle, in a transaction.- Returns:
- the value returned by the callback.
- Throws:
X- any exception thrown by the callback.- See Also:
-
inTransaction
public <R, X extends Exception> R inTransaction(Handle handle, TransactionIsolationLevel level, HandleCallback<R, X> callback) throws XDescription copied from interface:TransactionHandlerRun a transaction.- Specified by:
inTransactionin interfaceTransactionHandler- Type Parameters:
R- the callback return typeX- the exception type thrown by the callback, if any- Parameters:
handle- the handle to the databaselevel- the isolation level for the transactioncallback- a callback which will receive the open handle, in a transaction.- Returns:
- the value returned by the callback.
- Throws:
X- any exception thrown by the callback.- See Also:
-
binding
-
managed
Returns a handler that manages transactions itself on every connection, including a connection with autocommit disabled. The handler does not read the autocommit flag to find the transaction state. It tracks the transactions that Jdbi demarcates:inTransactionanduseTransactionstart a transaction, run the callback, and commit, andisInTransactionreturns true only betweenbeginand the commit or rollback. A statement executed outside of a transaction is not committed, andcommitorrollbackoutside of a transaction does not touch the connection.Use this handler when a connection pool disables autocommit as a precaution and Jdbi manages the transactions, for example
jdbi.setTransactionHandler(LocalTransactionHandler.managed()).- Returns:
- a transaction handler that manages transactions on connections with autocommit disabled.
-
reset
-