Class TransactionUtil

  • All Implemented Interfaces:
    javax.transaction.Status

    public final class TransactionUtil
    extends java.lang.Object
    implements javax.transaction.Status

    Transaction Utility to help with some common transaction tasks

    Provides a wrapper around the transaction objects to allow for changes in underlying implementations in the future.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.util.Map<javax.transaction.xa.Xid,​DebugXaResource> debugResMap  
      static java.lang.String module  
      • Fields inherited from interface javax.transaction.Status

        STATUS_ACTIVE, STATUS_COMMITTED, STATUS_COMMITTING, STATUS_MARKED_ROLLBACK, STATUS_NO_TRANSACTION, STATUS_PREPARED, STATUS_PREPARING, STATUS_ROLLEDBACK, STATUS_ROLLING_BACK, STATUS_UNKNOWN
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean begin()
      Begins a transaction in the current thread IF transactions are available; only tries if the current transaction status is ACTIVE, if not active it returns false.
      static boolean begin​(int timeout)
      Begins a transaction in the current thread IF transactions are available; only tries if the current transaction status is ACTIVE, if not active it returns false.
      static int cleanSuspendedTransactions()
      BE VERY CAREFUL WHERE YOU CALL THIS!!
      protected static void clearTransactionStamps()  
      static void commit()
      Commits the transaction in the current thread IF transactions are available
      static void commit​(boolean beganTransaction)
      Commits the transaction in the current thread IF transactions are available AND if beganTransaction is true
      static boolean debugResources()  
      static <V> V doNewTransaction​(java.util.concurrent.Callable<V> callable, java.lang.String ifErrorMessage, int timeout, boolean printException)  
      static <V> V doTransaction​(java.util.concurrent.Callable<V> callable, java.lang.String ifErrorMessage, int timeout, boolean printException)  
      static java.sql.Connection enlistConnection​(javax.sql.XAConnection xacon)
      Enlists the given XAConnection and if a transaction is active in the current thread, returns a plain JDBC Connection
      static void enlistResource​(javax.transaction.xa.XAResource resource)  
      static java.util.Map<java.lang.Long,​java.util.List<java.lang.Exception>> getAllThreadsTransactionBeginStackSave()  
      static java.sql.Connection getCursorConnection​(GenericHelperInfo helperInfo, java.sql.Connection con)  
      static org.apache.ofbiz.entity.transaction.TransactionUtil.RollbackOnlyCause getSetRollbackOnlyCause()  
      static int getStatus()
      Gets the status of the transaction in the current thread IF transactions are available, otherwise returns STATUS_NO_TRANSACTION
      static java.lang.String getStatusString()  
      static java.util.List<java.lang.Exception> getSuspendedTxLocationsStack()  
      static java.util.List<javax.transaction.Transaction> getSuspendedTxStack()  
      static java.lang.Exception getTransactionBeginStack()  
      static java.util.List<java.lang.Exception> getTransactionBeginStackSave()  
      static int getTransactionBeginStackSaveSize()  
      static java.sql.Timestamp getTransactionStartStamp()  
      static java.lang.String getTransactionStateString​(int state)  
      static java.sql.Timestamp getTransactionUniqueNowStamp()  
      protected static void internalBegin​(javax.transaction.UserTransaction ut, int timeout)  
      static <V> TransactionUtil.InTransaction<V> inTransaction​(java.util.concurrent.Callable<V> callable, java.lang.String ifErrorMessage, int timeout, boolean printException)  
      static boolean isTransactionInPlace()  
      static void logRunningTx()  
      static <V> TransactionUtil.NoTransaction<V> noTransaction​(java.util.concurrent.Callable<V> callable)  
      protected static javax.transaction.Transaction popSuspendedTransaction()  
      static void printAllThreadsTransactionBeginStacks()  
      protected static void pushSuspendedTransaction​(javax.transaction.Transaction t)  
      static void registerSynchronization​(javax.transaction.Synchronization sync)  
      protected static void removeSuspendedTransaction​(javax.transaction.Transaction t)  
      static void resume​(javax.transaction.Transaction parentTx)  
      static void rollback()
      Rolls back transaction in the current thread IF transactions are available
      static void rollback​(boolean beganTransaction, java.lang.String causeMessage, java.lang.Throwable causeThrowable)
      Rolls back transaction in the current thread IF transactions are available AND if beganTransaction is true; if beganTransaction is not true, setRollbackOnly is called to insure that the transaction will be rolled back
      static void rollback​(java.lang.Throwable causeThrowable)
      Rolls back transaction in the current thread IF transactions are available
      static void setRollbackOnly​(java.lang.String causeMessage, java.lang.Throwable causeThrowable)
      Makes a rollback the only possible outcome of the transaction in the current thread IF transactions are available
      static void setTransactionTimeout​(int seconds)
      Sets the timeout of the transaction in the current thread IF transactions are available
      static javax.transaction.Transaction suspend()  
      static boolean suspendedTransactionsHeld()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • module

        public static final java.lang.String module
      • debugResMap

        public static final java.util.Map<javax.transaction.xa.Xid,​DebugXaResource> debugResMap
    • Method Detail

      • doNewTransaction

        public static <V> V doNewTransaction​(java.util.concurrent.Callable<V> callable,
                                             java.lang.String ifErrorMessage,
                                             int timeout,
                                             boolean printException)
                                      throws GenericEntityException
        Throws:
        GenericEntityException
      • doTransaction

        public static <V> V doTransaction​(java.util.concurrent.Callable<V> callable,
                                          java.lang.String ifErrorMessage,
                                          int timeout,
                                          boolean printException)
                                   throws GenericEntityException
        Throws:
        GenericEntityException
      • inTransaction

        public static <V> TransactionUtil.InTransaction<V> inTransaction​(java.util.concurrent.Callable<V> callable,
                                                                         java.lang.String ifErrorMessage,
                                                                         int timeout,
                                                                         boolean printException)
      • begin

        public static boolean begin()
                             throws GenericTransactionException
        Begins a transaction in the current thread IF transactions are available; only tries if the current transaction status is ACTIVE, if not active it returns false. If and on only if it begins a transaction it will return true. In other words, if a transaction is already in place it will return false and do nothing.
        Throws:
        GenericTransactionException
      • begin

        public static boolean begin​(int timeout)
                             throws GenericTransactionException
        Begins a transaction in the current thread IF transactions are available; only tries if the current transaction status is ACTIVE, if not active it returns false. If and on only if it begins a transaction it will return true. In other words, if a transaction is already in place it will return false and do nothing.
        Throws:
        GenericTransactionException
      • internalBegin

        protected static void internalBegin​(javax.transaction.UserTransaction ut,
                                            int timeout)
                                     throws javax.transaction.SystemException,
                                            javax.transaction.NotSupportedException
        Throws:
        javax.transaction.SystemException
        javax.transaction.NotSupportedException
      • rollback

        public static void rollback​(boolean beganTransaction,
                                    java.lang.String causeMessage,
                                    java.lang.Throwable causeThrowable)
                             throws GenericTransactionException
        Rolls back transaction in the current thread IF transactions are available AND if beganTransaction is true; if beganTransaction is not true, setRollbackOnly is called to insure that the transaction will be rolled back
        Throws:
        GenericTransactionException
      • setRollbackOnly

        public static void setRollbackOnly​(java.lang.String causeMessage,
                                           java.lang.Throwable causeThrowable)
                                    throws GenericTransactionException
        Makes a rollback the only possible outcome of the transaction in the current thread IF transactions are available
        Throws:
        GenericTransactionException
      • enlistConnection

        public static java.sql.Connection enlistConnection​(javax.sql.XAConnection xacon)
                                                    throws GenericTransactionException
        Enlists the given XAConnection and if a transaction is active in the current thread, returns a plain JDBC Connection
        Throws:
        GenericTransactionException
      • getTransactionStateString

        public static java.lang.String getTransactionStateString​(int state)
      • debugResources

        public static boolean debugResources()
      • logRunningTx

        public static void logRunningTx()
      • suspendedTransactionsHeld

        public static boolean suspendedTransactionsHeld()
      • getSuspendedTxStack

        public static java.util.List<javax.transaction.Transaction> getSuspendedTxStack()
      • getSuspendedTxLocationsStack

        public static java.util.List<java.lang.Exception> getSuspendedTxLocationsStack()
      • pushSuspendedTransaction

        protected static void pushSuspendedTransaction​(javax.transaction.Transaction t)
      • popSuspendedTransaction

        protected static javax.transaction.Transaction popSuspendedTransaction()
      • removeSuspendedTransaction

        protected static void removeSuspendedTransaction​(javax.transaction.Transaction t)
      • getTransactionBeginStackSaveSize

        public static int getTransactionBeginStackSaveSize()
      • getTransactionBeginStackSave

        public static java.util.List<java.lang.Exception> getTransactionBeginStackSave()
      • getAllThreadsTransactionBeginStackSave

        public static java.util.Map<java.lang.Long,​java.util.List<java.lang.Exception>> getAllThreadsTransactionBeginStackSave()
      • printAllThreadsTransactionBeginStacks

        public static void printAllThreadsTransactionBeginStacks()
      • getTransactionBeginStack

        public static java.lang.Exception getTransactionBeginStack()
      • getSetRollbackOnlyCause

        public static org.apache.ofbiz.entity.transaction.TransactionUtil.RollbackOnlyCause getSetRollbackOnlyCause()
      • getTransactionStartStamp

        public static java.sql.Timestamp getTransactionStartStamp()
      • getTransactionUniqueNowStamp

        public static java.sql.Timestamp getTransactionUniqueNowStamp()
      • clearTransactionStamps

        protected static void clearTransactionStamps()
      • getCursorConnection

        public static java.sql.Connection getCursorConnection​(GenericHelperInfo helperInfo,
                                                              java.sql.Connection con)