Package org.nuxeo.runtime.transaction
Class TransactionHelper
java.lang.Object
org.nuxeo.runtime.transaction.TransactionHelper
Utilities to work with transactions.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static class -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final ExecutorServiceThread pool used to execute code in a separate transactional context. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidChecks if the current User Transaction has already timed out, i.e., whether a commit would immediately abort with a timeout exception.static voidCommits or rolls back the User Transaction depending on the transaction status.static voidenlistResource(XAResource xaRes) Enlists a XA resource in the current transaction.protected static TransactionHelper.GeronimoTransactionInfoGets the suppressed exceptions, and stops remembering.static intGets the transaction status.static intGets the transaction TTL.static StringReturns the UserTransaction JNDI binding name.static booleanChecks if there is no transactionstatic booleanChecks if the current User Transaction is active.static booleanChecks if the current User Transaction is active or marked rollback only.static booleanChecks if the current User Transaction is active or preparing.static booleanChecks if the current User Transaction is marked rollback only.static booleanChecks if the current User Transaction has already timed out, i.e., whether a commit would immediately abort with a timeout exception.static jakarta.transaction.TransactionSynchronizationRegistryLooks up the TransactionSynchronizationRegistry in JNDI.static jakarta.transaction.TransactionManagerLooks up the TransactionManager in JNDI.static jakarta.transaction.UserTransactionLooks up the User Transaction in JNDI.protected static <R> RnameTransactionThread(Supplier<R> supplier, String callerThreadName) static voidRemembers the exception if it happens during the processing of a commit, so that it can be surfaced as a suppressed exception at the end of the commit.protected static voidAfter this, some exceptions during transaction commit may be suppressed and remembered.static voidregisterInterposedSynchronization(jakarta.transaction.Synchronization handler) static voidregisterSynchronization(jakarta.transaction.Synchronization handler) static jakarta.transaction.TransactionDeprecated.static voidresumeTransaction(jakarta.transaction.Transaction tx) Deprecated.since 11.1, as not all backends (transaction resource managers) allow suspending the transaction or transaction interleaving, instead userunInNewTransaction(java.lang.Runnable)orrunWithoutTransaction(java.lang.Runnable)explicitlyprotected static <R> RrunAndCleanupTransactionContext(Supplier<R> supplier) Calls the givenSupplierand cleans up the transaction context afterwards.static voidrunInNewTransaction(Runnable runnable) Runs the givenRunnablein a new transactional context.static <R> RrunInNewTransaction(Supplier<R> supplier) Calls the givenSupplierin a new transactional context.static voidrunInTransaction(int timeout, Runnable runnable) Runs the givenRunnablein a transactional context.static <R> RrunInTransaction(int timeout, Supplier<R> supplier) Calls the givenSupplierin a transactional context.static voidrunInTransaction(Runnable runnable) Runs the givenRunnablein a transactional context.static <R> RrunInTransaction(Supplier<R> supplier) Calls the givenSupplierin a transactional context.static voidrunWithoutTransaction(Runnable runnable) Runs the givenRunnablewithout a transactional context.static <R> RrunWithoutTransaction(Supplier<R> supplier) Calls the givenSupplierwithout a transactional context.protected static <R> RrunWithoutTransactionInternal(Supplier<R> supplier) Calls the givenSupplierin a context without a transaction.static booleanSets the current User Transaction as rollback only.static booleanSets the current User Transaction as rollback only if it has timed out.static booleanStarts a new User Transaction.static booleanstartTransaction(int timeout) Starts a new User Transaction with the specified timeout.static jakarta.transaction.TransactionDeprecated.since 11.1, as not all backends (transaction resource managers) allow suspending the transaction or transaction interleaving, instead userunInNewTransaction(java.lang.Runnable)orrunWithoutTransaction(java.lang.Runnable)explicitly
-
Field Details
-
EXECUTOR
Thread pool used to execute code in a separate transactional context.- Since:
- 11.1
-
-
Method Details
-
lookupUserTransaction
Looks up the User Transaction in JNDI.- Returns:
- the User Transaction
- Throws:
NamingException- if not found
-
getTransactionStatus
public static int getTransactionStatus()Gets the transaction status.- Returns:
- the transaction status, or -1 if there is no transaction manager
- Since:
- 8.4
- See Also:
-
getUserTransactionJNDIName
Returns the UserTransaction JNDI binding name.Assumes
lookupUserTransaction()has been called once before. -
lookupTransactionManager
public static jakarta.transaction.TransactionManager lookupTransactionManager() throws NamingExceptionLooks up the TransactionManager in JNDI.- Returns:
- the TransactionManager
- Throws:
NamingException- if not found
-
lookupSynchronizationRegistry
public static jakarta.transaction.TransactionSynchronizationRegistry lookupSynchronizationRegistry() throws NamingExceptionLooks up the TransactionSynchronizationRegistry in JNDI.- Returns:
- the TransactionSynchronizationRegistry
- Throws:
NamingException- if not found
-
isNoTransaction
public static boolean isNoTransaction()Checks if there is no transaction- Since:
- 6.0
-
isTransactionActive
public static boolean isTransactionActive()Checks if the current User Transaction is active. -
isTransactionMarkedRollback
public static boolean isTransactionMarkedRollback()Checks if the current User Transaction is marked rollback only. -
isTransactionActiveOrMarkedRollback
public static boolean isTransactionActiveOrMarkedRollback()Checks if the current User Transaction is active or marked rollback only. -
isTransactionActiveOrPreparing
public static boolean isTransactionActiveOrPreparing()Checks if the current User Transaction is active or preparing.- Since:
- 8.4
-
isTransactionTimedOut
public static boolean isTransactionTimedOut()Checks if the current User Transaction has already timed out, i.e., whether a commit would immediately abort with a timeout exception.- Returns:
trueif there is a current transaction that has timed out,falseotherwise- Since:
- 7.1
-
getGeronimoTransactionInfo
-
getTransactionTimeToLive
public static int getTransactionTimeToLive()Gets the transaction TTL.- Returns:
- the time to live in second,
0if the transaction has already timed out,-1when outside of a transaction. - Since:
- 11.5
-
checkTransactionTimeout
Checks if the current User Transaction has already timed out, i.e., whether a commit would immediately abort with a timeout exception.Throws if the transaction has timed out.
- Throws:
TransactionRuntimeException- if the transaction has timed out- Since:
- 8.4
-
startTransaction
public static boolean startTransaction()Starts a new User Transaction.- Returns:
trueif the transaction was successfully started,falseotherwise
-
requireNewTransaction
Deprecated.since 11.1, as not all backends (transaction resource managers) allow suspending the transaction or transaction interleaving, instead userunInNewTransaction(java.lang.Runnable)orrunWithoutTransaction(java.lang.Runnable)explicitlySuspend the current transaction if active and start a new transaction- Returns:
- the suspended transaction or null
- Since:
- 5.6
-
suspendTransaction
Deprecated.since 11.1, as not all backends (transaction resource managers) allow suspending the transaction or transaction interleaving, instead userunInNewTransaction(java.lang.Runnable)orrunWithoutTransaction(java.lang.Runnable)explicitlySuspends the current transaction and returns- Returns:
- the suspended transaction or null
-
resumeTransaction
Deprecated.since 11.1, as not all backends (transaction resource managers) allow suspending the transaction or transaction interleaving, instead userunInNewTransaction(java.lang.Runnable)orrunWithoutTransaction(java.lang.Runnable)explicitlyCommit the current transaction if active and resume the principal transaction -
startTransaction
public static boolean startTransaction(int timeout) Starts a new User Transaction with the specified timeout.- Parameters:
timeout- the timeout in seconds, <= 0 for the default- Returns:
trueif the transaction was successfully started,falseotherwise- Since:
- 5.6
-
commitOrRollbackTransaction
public static void commitOrRollbackTransaction()Commits or rolls back the User Transaction depending on the transaction status. -
noteSuppressedExceptions
protected static void noteSuppressedExceptions()After this, some exceptions during transaction commit may be suppressed and remembered.- Since:
- 5.9.4
-
noteSuppressedException
Remembers the exception if it happens during the processing of a commit, so that it can be surfaced as a suppressed exception at the end of the commit.- Since:
- 5.9.4
-
getSuppressedExceptions
Gets the suppressed exceptions, and stops remembering.- Since:
- 5.9.4
-
setTransactionRollbackOnly
public static boolean setTransactionRollbackOnly()Sets the current User Transaction as rollback only.- Returns:
trueif the transaction was successfully marked rollback only,falseotherwise
-
setTransactionRollbackOnlyIfTimedOut
public static boolean setTransactionRollbackOnlyIfTimedOut()Sets the current User Transaction as rollback only if it has timed out.- Returns:
trueif the transaction was successfully marked rollback only,falseotherwise- Since:
- 7.1
-
registerSynchronization
public static void registerSynchronization(jakarta.transaction.Synchronization handler) -
registerInterposedSynchronization
public static void registerInterposedSynchronization(jakarta.transaction.Synchronization handler) -
enlistResource
Enlists a XA resource in the current transaction.- Parameters:
xaRes- the XA resource- Since:
- 11.1
-
runWithoutTransaction
Runs the givenRunnablewithout a transactional context.- Parameters:
runnable- theRunnable- Since:
- 9.1
-
runWithoutTransaction
Calls the givenSupplierwithout a transactional context.- Parameters:
supplier- theSupplier- Returns:
- the supplier's result
- Since:
- 9.1
-
runInNewTransaction
Runs the givenRunnablein a new transactional context.- Parameters:
runnable- theRunnable- Since:
- 9.1
-
runInNewTransaction
Calls the givenSupplierin a new transactional context.- Parameters:
supplier- theSupplier- Returns:
- the supplier's result
- Since:
- 9.1
-
runInTransaction
Runs the givenRunnablein a transactional context. Will not start a new transaction if one already exists.- Parameters:
runnable- theRunnable- Since:
- 8.4
-
runInTransaction
Runs the givenRunnablein a transactional context. Will not start a new transaction if one already exists.- Parameters:
timeout- the timeout in seconds, <= 0 for the defaultrunnable- theRunnable- Since:
- 11.5
-
runInTransaction
Calls the givenSupplierin a transactional context. Will not start a new transaction if one already exists.- Parameters:
supplier- theSupplier- Returns:
- the supplier's result
- Since:
- 8.4
-
runInTransaction
Calls the givenSupplierin a transactional context. Will not start a new transaction if one already exists.- Parameters:
timeout- the timeout in seconds, <= 0 for the defaultsupplier- theSupplier- Returns:
- the supplier's result
- Since:
- 11.5
-
runWithoutTransactionInternal
Calls the givenSupplierin a context without a transaction. The supplier must do its own transactional cleanup to restore the thread to a pristine state.- Parameters:
supplier- theSupplier- Returns:
- the supplier's result
- Since:
- 11.1
-
nameTransactionThread
-
runAndCleanupTransactionContext
Calls the givenSupplierand cleans up the transaction context afterwards.- Parameters:
supplier- theSupplier- Returns:
- the supplier's result
- Since:
- 11.1
-
runInNewTransaction(java.lang.Runnable)orrunWithoutTransaction(java.lang.Runnable)explicitly