Interface Transaction
- All Superinterfaces:
EntityTransaction
- All Known Subinterfaces:
TransactionImplementor
Every resource-local transaction is associated with a Session and begins with
an explicit call to SharedSessionContract.beginTransaction(), or, almost equivalently, with
session.getTransaction().begin(), and ends with a call to EntityTransaction.commit()
or EntityTransaction.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 given Session 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.
A Transaction object is not threadsafe.
- See Also:
- API Note:
- JPA doesn't allow an
EntityTransactionto represent a JTA transaction. But when strict 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 current status of this transaction.@Nullable IntegerRetrieve 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 a synchronization 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 current status 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 been marked 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 the status isTransactionStatus.ACTIVEorTransactionStatus.MARKED_ROLLBACK
-
isInCompletionProcess
Is this transaction currently in the completion process?Note that a
Synchronizationis called before and after the completion process. Therefore, this state is not usually observable to the client program logic.- Returns:
trueif the status isTransactionStatus.COMMITTINGorTransactionStatus.ROLLING_BACK- Since:
- 7.0
-
isComplete
Is this transaction complete?- Returns:
trueif the status isTransactionStatus.COMMITTED,TransactionStatus.ROLLED_BACK,TransactionStatus.FAILED_COMMIT, orTransactionStatus.FAILED_ROLLBACK- Since:
- 7.0
-
wasStarted
Was this transaction already started?- Returns:
trueif the status is anything other thanTransactionStatus.NOT_ACTIVE- Since:
- 7.0
-
wasSuccessful
Was this transaction already successfully committed?- Returns:
trueif the status 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 the status 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 a synchronization 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
@Nullable Integer 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:
-