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 class
TransactionHelper.GeronimoTransactionInfo
-
Field Summary
Fields Modifier and Type Field Description protected static ExecutorService
EXECUTOR
Thread 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 void
checkTransactionTimeout()
Checks if the current User Transaction has already timed out, i.e., whether a commit would immediately abort with a timeout exception.static void
commitOrRollbackTransaction()
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
getGeronimoTransactionInfo()
protected static List<Exception>
getSuppressedExceptions()
Gets the suppressed exceptions, and stops remembering.static int
getTransactionStatus()
Gets the transaction status.static int
getTransactionTimeToLive()
Gets the transaction TTL.static String
getUserTransactionJNDIName()
Returns the UserTransaction JNDI binding name.static boolean
isNoTransaction()
Checks if there is no transactionstatic boolean
isTransactionActive()
Checks if the current User Transaction is active.static boolean
isTransactionActiveOrMarkedRollback()
Checks if the current User Transaction is active or marked rollback only.static boolean
isTransactionActiveOrPreparing()
Checks if the current User Transaction is active or preparing.static boolean
isTransactionMarkedRollback()
Checks if the current User Transaction is marked rollback only.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.static javax.transaction.TransactionSynchronizationRegistry
lookupSynchronizationRegistry()
Looks up the TransactionSynchronizationRegistry in JNDI.static javax.transaction.TransactionManager
lookupTransactionManager()
Looks up the TransactionManager in JNDI.static javax.transaction.UserTransaction
lookupUserTransaction()
Looks up the User Transaction in JNDI.protected static <R> R
nameTransactionThread(Supplier<R> supplier, String callerThreadName)
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.protected static void
noteSuppressedExceptions()
After this, some exceptions during transaction commit may be suppressed and remembered.static void
registerSynchronization(javax.transaction.Synchronization handler)
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)
explicitlystatic 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
setTransactionRollbackOnly()
Sets the current User Transaction as rollback only.static boolean
setTransactionRollbackOnlyIfTimedOut()
Sets the current User Transaction as rollback only if it has timed out.static boolean
startTransaction()
Starts a new User Transaction.static boolean
startTransaction(int timeout)
Starts a new User Transaction with the specified timeout.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)
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 NamingException
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
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 NamingException
Looks up the TransactionManager in JNDI.- Returns:
- the TransactionManager
- Throws:
NamingException
- if not found
-
lookupSynchronizationRegistry
public static javax.transaction.TransactionSynchronizationRegistry lookupSynchronizationRegistry() throws NamingException
Looks 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
protected static TransactionHelper.GeronimoTransactionInfo 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
public static void checkTransactionTimeout() throws TransactionRuntimeException
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") 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:
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
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:
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)
-
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 givenRunnable
without a transactional context.- Parameters:
runnable
- theRunnable
- Since:
- 9.1
-
runWithoutTransaction
public static <R> R runWithoutTransaction(Supplier<R> supplier)
Calls the givenSupplier
without a transactional context.- Parameters:
supplier
- theSupplier
- Returns:
- the supplier's result
- Since:
- 9.1
-
runInNewTransaction
public static void runInNewTransaction(Runnable runnable)
Runs the givenRunnable
in a new transactional context.- Parameters:
runnable
- theRunnable
- Since:
- 9.1
-
runInNewTransaction
public static <R> R runInNewTransaction(Supplier<R> supplier)
Calls the givenSupplier
in a new transactional context.- Parameters:
supplier
- theSupplier
- Returns:
- the supplier's result
- Since:
- 9.1
-
runInTransaction
public static void runInTransaction(Runnable runnable)
Runs the givenRunnable
in 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 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
public static <R> R runInTransaction(Supplier<R> supplier)
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
public static <R> R runInTransaction(int timeout, Supplier<R> supplier)
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
protected static <R> R runWithoutTransactionInternal(Supplier<R> supplier)
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
protected static <R> R nameTransactionThread(Supplier<R> supplier, String callerThreadName)
-
-