Package org.nuxeo.runtime.transaction
Class TransactionHelper
- java.lang.Object
-
- org.nuxeo.runtime.transaction.TransactionHelper
-
public class TransactionHelper extends Object
Utilities to work with transactions.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classTransactionHelper.GeronimoTransactionInfo
-
Field Summary
Fields Modifier and Type Field Description protected static ExecutorServiceEXECUTORThread pool used to execute code in a separate transactional context.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static voidcheckTransactionTimeout()Checks if the current User Transaction has already timed out, i.e., whether a commit would immediately abort with a timeout exception.static voidcommitOrRollbackTransaction()Commits 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.GeronimoTransactionInfogetGeronimoTransactionInfo()protected static List<Exception>getSuppressedExceptions()Gets the suppressed exceptions, and stops remembering.static intgetTransactionStatus()Gets the transaction status.static intgetTransactionTimeToLive()Gets the transaction TTL.static StringgetUserTransactionJNDIName()Returns the UserTransaction JNDI binding name.static booleanisNoTransaction()Checks if there is no transactionstatic booleanisTransactionActive()Checks if the current User Transaction is active.static booleanisTransactionActiveOrMarkedRollback()Checks if the current User Transaction is active or marked rollback only.static booleanisTransactionActiveOrPreparing()Checks if the current User Transaction is active or preparing.static booleanisTransactionMarkedRollback()Checks if the current User Transaction is marked rollback only.static booleanisTransactionTimedOut()Checks if the current User Transaction has already timed out, i.e., whether a commit would immediately abort with a timeout exception.static javax.transaction.TransactionSynchronizationRegistrylookupSynchronizationRegistry()Looks up the TransactionSynchronizationRegistry in JNDI.static javax.transaction.TransactionManagerlookupTransactionManager()Looks up the TransactionManager in JNDI.static javax.transaction.UserTransactionlookupUserTransaction()Looks up the User Transaction in JNDI.protected static <R> RnameTransactionThread(Supplier<R> supplier, String callerThreadName)static voidnoteSuppressedException(Exception e)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.protected static voidnoteSuppressedExceptions()After this, some exceptions during transaction commit may be suppressed and remembered.static voidregisterSynchronization(javax.transaction.Synchronization handler)static javax.transaction.TransactionrequireNewTransaction()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)explicitlystatic voidresumeTransaction(javax.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 booleansetTransactionRollbackOnly()Sets the current User Transaction as rollback only.static booleansetTransactionRollbackOnlyIfTimedOut()Sets the current User Transaction as rollback only if it has timed out.static booleanstartTransaction()Starts a new User Transaction.static booleanstartTransaction(int timeout)Starts a new User Transaction with the specified timeout.static javax.transaction.TransactionsuspendTransaction()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)explicitly
-
-
-
Field Detail
-
EXECUTOR
protected static final ExecutorService EXECUTOR
Thread pool used to execute code in a separate transactional context.- Since:
- 11.1
-
-
Method Detail
-
lookupUserTransaction
public static javax.transaction.UserTransaction lookupUserTransaction() throws NamingExceptionLooks 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:
Status
-
getUserTransactionJNDIName
public static String getUserTransactionJNDIName()
Returns the UserTransaction JNDI binding name.Assumes
lookupUserTransaction()has been called once before.
-
lookupTransactionManager
public static javax.transaction.TransactionManager lookupTransactionManager() throws NamingExceptionLooks up the TransactionManager in JNDI.- Returns:
- the TransactionManager
- Throws:
NamingException- if not found
-
lookupSynchronizationRegistry
public static javax.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
protected static TransactionHelper.GeronimoTransactionInfo 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
public static void checkTransactionTimeout() throws TransactionRuntimeExceptionChecks 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") public static javax.transaction.Transaction 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") public static javax.transaction.Transaction 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") public static void resumeTransaction(javax.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)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
public static void noteSuppressedException(Exception e)
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
protected static List<Exception> 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(javax.transaction.Synchronization handler)
-
enlistResource
public static void enlistResource(XAResource xaRes)
Enlists a XA resource in the current transaction.- Parameters:
xaRes- the XA resource- Since:
- 11.1
-
runWithoutTransaction
public static void runWithoutTransaction(Runnable runnable)
Runs the givenRunnablewithout a transactional context.- Parameters:
runnable- theRunnable- Since:
- 9.1
-
runWithoutTransaction
public static <R> R runWithoutTransaction(Supplier<R> supplier)
Calls the givenSupplierwithout a transactional context.- Parameters:
supplier- theSupplier- Returns:
- the supplier's result
- Since:
- 9.1
-
runInNewTransaction
public static void runInNewTransaction(Runnable runnable)
Runs the givenRunnablein a new transactional context.- Parameters:
runnable- theRunnable- Since:
- 9.1
-
runInNewTransaction
public static <R> R runInNewTransaction(Supplier<R> supplier)
Calls the givenSupplierin a new transactional context.- Parameters:
supplier- theSupplier- Returns:
- the supplier's result
- Since:
- 9.1
-
runInTransaction
public static void runInTransaction(Runnable runnable)
Runs the givenRunnablein a transactional context. Will not start a new transaction if one already exists.- Parameters:
runnable- theRunnable- Since:
- 8.4
-
runInTransaction
public static void runInTransaction(int timeout, Runnable runnable)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
public static <R> R runInTransaction(Supplier<R> supplier)
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
public static <R> R runInTransaction(int timeout, Supplier<R> supplier)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
protected static <R> R runWithoutTransactionInternal(Supplier<R> supplier)
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
protected static <R> R nameTransactionThread(Supplier<R> supplier, String callerThreadName)
-
-