Package org.nuxeo.runtime.transaction
Class TransactionHelper
java.lang.Object
org.nuxeo.runtime.transaction.TransactionHelper
Utilities to work with transactions.
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
-
Field Summary
Modifier and TypeFieldDescriptionprotected static final ExecutorService
Thread pool used to execute code in a separate transactional context. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Checks if the current User Transaction has already timed out, i.e., whether a commit would immediately abort with a timeout exception.static void
Commits or rolls back the User Transaction depending on the transaction status.static void
enlistResource
(XAResource xaRes) Enlists a XA resource in the current transaction.protected static TransactionHelper.GeronimoTransactionInfo
Gets the suppressed exceptions, and stops remembering.static int
Gets the transaction status.static int
Gets the transaction TTL.static String
Returns the UserTransaction JNDI binding name.static boolean
Checks if there is no transactionstatic boolean
Checks if the current User Transaction is active.static boolean
Checks if the current User Transaction is active or marked rollback only.static boolean
Checks if the current User Transaction is active or preparing.static boolean
Checks if the current User Transaction is marked rollback only.static boolean
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.TransactionSynchronizationRegistry
Looks up the TransactionSynchronizationRegistry in JNDI.static javax.transaction.TransactionManager
Looks up the TransactionManager in JNDI.static javax.transaction.UserTransaction
Looks up the User Transaction in JNDI.protected static <R> R
nameTransactionThread
(Supplier<R> supplier, String callerThreadName) static void
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 void
After this, some exceptions during transaction commit may be suppressed and remembered.static void
registerInterposedSynchronization
(javax.transaction.Synchronization handler) static void
registerSynchronization
(javax.transaction.Synchronization handler) static javax.transaction.Transaction
Deprecated.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)
explicitlyprotected static <R> R
runAndCleanupTransactionContext
(Supplier<R> supplier) Calls the givenSupplier
and cleans up the transaction context afterwards.static void
runInNewTransaction
(Runnable runnable) Runs the givenRunnable
in a new transactional context.static <R> R
runInNewTransaction
(Supplier<R> supplier) Calls the givenSupplier
in a new transactional context.static void
runInTransaction
(int timeout, Runnable runnable) Runs the givenRunnable
in a transactional context.static <R> R
runInTransaction
(int timeout, Supplier<R> supplier) Calls the givenSupplier
in a transactional context.static void
runInTransaction
(Runnable runnable) Runs the givenRunnable
in a transactional context.static <R> R
runInTransaction
(Supplier<R> supplier) Calls the givenSupplier
in a transactional context.static void
runWithoutTransaction
(Runnable runnable) Runs the givenRunnable
without a transactional context.static <R> R
runWithoutTransaction
(Supplier<R> supplier) Calls the givenSupplier
without a transactional context.protected static <R> R
runWithoutTransactionInternal
(Supplier<R> supplier) Calls the givenSupplier
in a context without a transaction.static boolean
Sets the current User Transaction as rollback only.static boolean
Sets the current User Transaction as rollback only if it has timed out.static boolean
Starts a new User Transaction.static boolean
startTransaction
(int timeout) Starts a new User Transaction with the specified timeout.static javax.transaction.Transaction
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 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:
-
Status
-
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:
true
if there is a current transaction that has timed out,false
otherwise- Since:
- 7.1
-
getGeronimoTransactionInfo
-
getTransactionTimeToLive
public static int getTransactionTimeToLive()Gets the transaction TTL.- Returns:
- the time to live in second,
0
if the transaction has already timed out,-1
when 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:
true
if the transaction was successfully started,false
otherwise
-
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:
true
if the transaction was successfully started,false
otherwise- 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:
true
if the transaction was successfully marked rollback only,false
otherwise
-
setTransactionRollbackOnlyIfTimedOut
public static boolean setTransactionRollbackOnlyIfTimedOut()Sets the current User Transaction as rollback only if it has timed out.- Returns:
true
if the transaction was successfully marked rollback only,false
otherwise- Since:
- 7.1
-
registerSynchronization
public static void registerSynchronization(javax.transaction.Synchronization handler) -
registerInterposedSynchronization
public static void registerInterposedSynchronization(javax.transaction.Synchronization handler) -
enlistResource
Enlists a XA resource in the current transaction.- Parameters:
xaRes
- the XA resource- Since:
- 11.1
-
runWithoutTransaction
Runs the givenRunnable
without a transactional context.- Parameters:
runnable
- theRunnable
- Since:
- 9.1
-
runWithoutTransaction
Calls the givenSupplier
without a transactional context.- Parameters:
supplier
- theSupplier
- Returns:
- the supplier's result
- Since:
- 9.1
-
runInNewTransaction
Runs the givenRunnable
in a new transactional context.- Parameters:
runnable
- theRunnable
- Since:
- 9.1
-
runInNewTransaction
Calls the givenSupplier
in a new transactional context.- Parameters:
supplier
- theSupplier
- Returns:
- the supplier's result
- Since:
- 9.1
-
runInTransaction
Runs the givenRunnable
in a transactional context. Will not start a new transaction if one already exists.- Parameters:
runnable
- theRunnable
- Since:
- 8.4
-
runInTransaction
Runs the givenRunnable
in 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 givenSupplier
in 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 givenSupplier
in 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 givenSupplier
in 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 givenSupplier
and 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