Interface Transaction
- All Superinterfaces:
EntityTransaction
- All Known Subinterfaces:
TransactionImplementor
- All Known Implementing Classes:
TransactionImpl
Every resource-local transaction is associated with aSession and begins with an explicit call toSharedSessionContract.beginTransaction(), or, almost equivalently, withsession.getTransaction().begin(), and ends with a call toEntityTransaction.commit() orEntityTransaction.rollback().
A single session might span multiple transactions since the notion of a session (a conversation between the application and the datastore) is of coarser granularity than the concept of a database transaction. However, there is at most one uncommitted transaction associated with a givenSession at any time.
Note that this interface is never used to control container managed JTA transactions, and is not usually used to control transactions that affect multiple resources.
ATransaction object is not threadsafe.
- See Also:
- API Note:
- JPA doesn't allow an
EntityTransactionto represent a JTA transaction. But whenstrict JPA transaction compliance is disabled, as it is by default, Hibernate allows an instance of this interface to represent the current JTA transaction context.
Method Summary
Modifier and TypeMethodDescriptionGet the currentstatus of this transaction.@NullableIntegerRetrieve the transaction timeout set for this instance.default booleanisActive()Is this transaction still active?default booleanIs this transaction complete?default booleanIs this transaction currently in the completion process?voidAttempt to mark the underlying transaction for rollback only.voidregisterSynchronization(Synchronization synchronization) Register asynchronization callback for this transaction.default voidrunAfterCompletion(Consumer<TransactionStatus> action) Register an action which will be called during the "after completion" phase.default voidrunBeforeCompletion(Runnable action) Register an action which will be called during the "before completion" phase.voidsetTimeout(int seconds) Set the transaction timeout for any transaction started by a subsequent call toEntityTransaction.begin()on this instance ofTransaction.default booleanWas this transaction a failure? Here we consider a successful rollback, a failed commit, or a failed rollback to amount to transaction failure.default booleanWas this transaction already started?default booleanWas this transaction already successfully committed?Methods inherited from interface jakarta.persistence.EntityTransaction
begin,commit,getRollbackOnly,rollback,setRollbackOnly,setTimeout
Method Details
getStatus
TransactionStatus getStatus()Get the currentstatus of this transaction.- API Note:
TransactionStatusbelongs to an SPI package, and so this operation is a (fairly harmless) layer-breaker. Prefer the use ofisActive(),isComplete(),wasStarted(),wasSuccessful(),wasFailure(), orisInCompletionProcess()according to need.
isActive
default boolean isActive()Is this transaction still active?A transaction which has beenmarked for rollback is still considered active, and is still able to perform work. To determine if a transaction has been marked for rollback, call
EntityTransaction.getRollbackOnly().- Specified by:
isActivein interfaceEntityTransaction- Returns:
trueif thestatus isTransactionStatus.ACTIVEorTransactionStatus.MARKED_ROLLBACK
isInCompletionProcess
Is this transaction currently in the completion process?Note that a
Synchronizationis calledbefore andafter the completion process. Therefore, this state is not usually observable to the client program logic.- Returns:
trueif thestatus isTransactionStatus.COMMITTINGorTransactionStatus.ROLLING_BACK- Since:
- 7.0
isComplete
Is this transaction complete?- Returns:
trueif thestatus isTransactionStatus.COMMITTED,TransactionStatus.ROLLED_BACK,TransactionStatus.FAILED_COMMIT, orTransactionStatus.FAILED_ROLLBACK- Since:
- 7.0
wasStarted
Was this transaction already started?- Returns:
trueif thestatus is anything other thanTransactionStatus.NOT_ACTIVE- Since:
- 7.0
wasSuccessful
Was this transaction already successfully committed?- Returns:
trueif thestatus isTransactionStatus.COMMITTED- Since:
- 7.0
wasFailure
Was this transaction a failure? Here we consider a successful rollback, a failed commit, or a failed rollback to amount to transaction failure.- Returns:
trueif thestatus isTransactionStatus.ROLLED_BACK,TransactionStatus.FAILED_COMMIT,TransactionStatus.FAILED_ROLLBACK- Since:
- 7.0
runBeforeCompletion
Register an action which will be called during the "before completion" phase.- Since:
- 7.0
runAfterCompletion
Register an action which will be called during the "after completion" phase.- Since:
- 7.0
registerSynchronization
Register asynchronization callback for this transaction.- Parameters:
synchronization- TheSynchronizationcallback to register- API Note:
Synchronizationis a type defined by JTA, but this operation does not depend on the use of JTA for transaction management. Prefer the use of the methodsrunBeforeCompletion(java.lang.Runnable)andrunAfterCompletion(java.util.function.Consumer<org.hibernate.resource.transaction.spi.TransactionStatus>)for convenience.
setTimeout
void setTimeout(int seconds) Set the transaction timeout for any transaction started by a subsequent call toEntityTransaction.begin()on this instance ofTransaction.- Parameters:
seconds- The number of seconds before a timeout
getTimeout
@NullableInteger getTimeout()Retrieve the transaction timeout set for this instance.A
nullreturn value indicates that no timeout has been set.- Specified by:
getTimeoutin interfaceEntityTransaction- Returns:
- the timeout, in seconds, or
null
markRollbackOnly
void markRollbackOnly()Attempt to mark the underlying transaction for rollback only.Unlike
EntityTransaction.setRollbackOnly(), which is specified by JPA to throw when the transaction is inactive, this operation may be called on an inactive transaction, in which case it has no effect.- See Also: