Class TransactionHelper

java.lang.Object
org.nuxeo.runtime.transaction.TransactionHelper

public class TransactionHelper extends Object
Utilities to work with transactions.
  • Field Details

    • EXECUTOR

      protected static final ExecutorService EXECUTOR
      Thread pool used to execute code in a separate transactional context.
      Since:
      11.1
  • Method Details

    • 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 use runInNewTransaction(java.lang.Runnable) or runWithoutTransaction(java.lang.Runnable) explicitly
      Suspend 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 use runInNewTransaction(java.lang.Runnable) or runWithoutTransaction(java.lang.Runnable) explicitly
      Suspends 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 use runInNewTransaction(java.lang.Runnable) or runWithoutTransaction(java.lang.Runnable) explicitly
      Commit 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)
    • registerInterposedSynchronization

      public static void registerInterposedSynchronization(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 given Runnable without a transactional context.
      Parameters:
      runnable - the Runnable
      Since:
      9.1
    • runWithoutTransaction

      public static <R> R runWithoutTransaction(Supplier<R> supplier)
      Calls the given Supplier without a transactional context.
      Parameters:
      supplier - the Supplier
      Returns:
      the supplier's result
      Since:
      9.1
    • runInNewTransaction

      public static void runInNewTransaction(Runnable runnable)
      Runs the given Runnable in a new transactional context.
      Parameters:
      runnable - the Runnable
      Since:
      9.1
    • runInNewTransaction

      public static <R> R runInNewTransaction(Supplier<R> supplier)
      Calls the given Supplier in a new transactional context.
      Parameters:
      supplier - the Supplier
      Returns:
      the supplier's result
      Since:
      9.1
    • runInTransaction

      public static void runInTransaction(Runnable runnable)
      Runs the given Runnable in a transactional context. Will not start a new transaction if one already exists.
      Parameters:
      runnable - the Runnable
      Since:
      8.4
    • runInTransaction

      public static void runInTransaction(int timeout, Runnable runnable)
      Runs the given Runnable in a transactional context. Will not start a new transaction if one already exists.
      Parameters:
      timeout - the timeout in seconds, <= 0 for the default
      runnable - the Runnable
      Since:
      11.5
    • runInTransaction

      public static <R> R runInTransaction(Supplier<R> supplier)
      Calls the given Supplier in a transactional context. Will not start a new transaction if one already exists.
      Parameters:
      supplier - the Supplier
      Returns:
      the supplier's result
      Since:
      8.4
    • runInTransaction

      public static <R> R runInTransaction(int timeout, Supplier<R> supplier)
      Calls the given Supplier in a transactional context. Will not start a new transaction if one already exists.
      Parameters:
      timeout - the timeout in seconds, <= 0 for the default
      supplier - the Supplier
      Returns:
      the supplier's result
      Since:
      11.5
    • runWithoutTransactionInternal

      protected static <R> R runWithoutTransactionInternal(Supplier<R> supplier)
      Calls the given Supplier in a context without a transaction. The supplier must do its own transactional cleanup to restore the thread to a pristine state.
      Parameters:
      supplier - the Supplier
      Returns:
      the supplier's result
      Since:
      11.1
    • nameTransactionThread

      protected static <R> R nameTransactionThread(Supplier<R> supplier, String callerThreadName)
    • runAndCleanupTransactionContext

      protected static <R> R runAndCleanupTransactionContext(Supplier<R> supplier)
      Calls the given Supplier and cleans up the transaction context afterwards.
      Parameters:
      supplier - the Supplier
      Returns:
      the supplier's result
      Since:
      11.1