Class Trx

java.lang.Object
org.compiere.util.Trx

public class Trx extends Object
Transaction Management.
- Create new Transaction by Trx.get(name);
- ..transactions..
- commit();
---- start();
---- commit();
- close();
Author:
Jorg Janke, Low Heng Sin
  • added rollback(boolean) and commit(boolean) [20070105]
  • remove unnecessary use of savepoint
  • use UUID for safer transaction name generation, Teo Sarca, http://www.arhipac.ro
  • FR [ 2080217 ] Implement TrxRunnable
  • BF [ 2876927 ] Oracle JDBC driver problem https://sourceforge.net/p/adempiere/bugs/2173/, Teo Sarca, teo.sarca@gmail.com
  • BF [ 2849122 ] PO.AfterSave is not rollback on error - add releaseSavepoint method https://sourceforge.net/p/adempiere/bugs/2073/
    • Field Details

    • Method Details

      • get

        public static Trx get(String trxName, boolean createNew)
        Get or Create New Transaction
        Parameters:
        trxName - trx name
        createNew - if false, null is returned if trxName not an existing transaction
        Returns:
        Transaction or null
      • get

        @Deprecated public static Trx get(String trxName, boolean createNew, Connection con)
        Deprecated.
        Get Transaction in a Connection
        Parameters:
        trxName - trx name
        createNew - if false, null is returned if not found
        con - Connection
        Returns:
        Transaction or null
      • startTrxMonitor

        public static void startTrxMonitor()
        Start transaction timeout monitor (run every 5 minutes)
      • createTrxName

        public static String createTrxName(String prefix)
        Create unique Transaction Name
        Parameters:
        prefix - optional prefix
        Returns:
        unique transaction name
      • createTrxName

        public static String createTrxName()
        Create unique Transaction Name
        Returns:
        unique name
      • getConnection

        public Connection getConnection()
        Get connection
        Returns:
        connection
      • getConnection

        public Connection getConnection(boolean createNew)
        Get or Create New Connection
        Parameters:
        createNew - if true, create new connection if the trx does not have one created yet
        Returns:
        connection
      • getTrxName

        public String getTrxName()
        Get Transaction Name
        Returns:
        transaction name
      • start

        public boolean start()
        Start Trx
        Returns:
        true if trx started
      • getStartTime

        public Date getStartTime()
        Returns:
        The start time of this transaction
      • isActive

        public boolean isActive()
        Is Transaction Active
        Returns:
        true if transaction is active
      • rollback

        public boolean rollback(boolean throwException) throws SQLException
        Rollback transaction
        Parameters:
        throwException - if true, re-throws exception
        Returns:
        true if success, false if failed or transaction already rollback
        Throws:
        SQLException
      • rollback

        public boolean rollback()
        Rollback transaction
        Returns:
        true if success, false if failed or transaction already rollback
      • rollback

        public boolean rollback(Savepoint savepoint) throws SQLException
        Rollback to save point
        Parameters:
        savepoint -
        Returns:
        true if success, false if failed or transaction already rollback
        Throws:
        SQLException
      • commit

        public boolean commit(boolean throwException) throws SQLException
        Commit transaction
        Parameters:
        throwException - if true, re-throws exception
        Returns:
        true if success
        Throws:
        SQLException
      • commit

        public boolean commit()
        Commit transaction
        Returns:
        true if success
      • rollbackAndCloseOnTimeout

        public boolean rollbackAndCloseOnTimeout()
        Rollback and close transaction.
        This is means to be called by the timeout monitor and developer usually shouldn't call this directly.
        Returns:
        true if success
      • close

        public boolean close()
        End Transaction and Close Connection
        Returns:
        true if success
      • setSavepoint

        public Savepoint setSavepoint(String name) throws SQLException
        Set transaction save point
        Parameters:
        name - optional savepoint name
        Returns:
        Savepoint
        Throws:
        SQLException
      • setLastWFSavepoint

        public void setLastWFSavepoint(Savepoint savepoint)
        Set last workflow save point.
        For workflow engine use, developer usually shouldn't call this method directly.
        Parameters:
        savepoint -
      • getLastWFSavepoint

        public Savepoint getLastWFSavepoint()
        Get last set workflow save point.
        For workflow engine use, developer usually shouldn't call this method directly.
        Returns:
        last set workflow save point or null
      • releaseSavepoint

        public void releaseSavepoint(Savepoint savepoint) throws SQLException
        Release Transaction Savepoint
        Parameters:
        savepoint -
        Throws:
        SQLException
      • toString

        public String toString()
        String Representation
        Overrides:
        toString in class Object
        Returns:
        info
      • getOpenTransactions

        public static Trx[] getOpenTransactions()
        Get register transactions
        Returns:
        array of register transactions
      • getActiveTransactions

        public static Trx[] getActiveTransactions()
        Deprecated.
        - wrong method name fixed with IDEMPIERE-5355 - please use getOpenTransactions
        Returns:
        Trx[]
      • run

        public static void run(TrxRunnable r)
        See Also:
      • run

        public static void run(String trxName, TrxRunnable r)
        Execute runnable object using provided transaction.
        If execution fails, database operations will be rolled back.

        Example:

         Trx.run(null, new TrxRunnable() {
             public void run(String trxName) {
                 // do something using trxName
             }
         )};
         
        Parameters:
        trxName - transaction name (if null, a new transaction will be created)
        r - runnable object
        Throws:
        RuntimeException - or AdempiereException
      • getTimeout

        public int getTimeout()
        Get transaction time out value
        Returns:
        transaction time out value in second
      • setTimeout

        public void setTimeout(int timeout)
        set transaction timeout value ( in seconds )
        Parameters:
        timeout -
      • addTrxEventListener

        public void addTrxEventListener(TrxEventListener listener)
        Add transaction event listener
        Parameters:
        listener -
      • removeTrxEventListener

        public boolean removeTrxEventListener(TrxEventListener listener)
        Remove transaction event listener
        Parameters:
        listener -
        Returns:
        true if listener is found and remove
      • getStrackTrace

        public String getStrackTrace()
        Get stack trace save
        Returns:
        stack trace save or empty string
      • getDisplayName

        public String getDisplayName()
        Get transaction display name. Fall back to transaction name if display name is not set.
        Returns:
        display name or name
      • setDisplayName

        public void setDisplayName(String displayName)
        Set transaction display name
        Parameters:
        displayName -
      • setChangesMadeByEventListener

        public void setChangesMadeByEventListener(boolean changesMade)
        Indicate additional DB changes have been made by a transaction event listener
        Parameters:
        changesMade -
      • hasChangesMadeByEventListener

        public boolean hasChangesMadeByEventListener()
        Is there additional changes make by transaction event listener
        Returns:
        true if event listener(s) has flag that additional DB changes have been made
      • finalize

        protected void finalize() throws Throwable
        Overrides:
        finalize in class Object
        Throws:
        Throwable
      • registerNullTrx

        public static String registerNullTrx()
        Register a null trx
        Returns:
      • unregisterNullTrx

        public static void unregisterNullTrx(String nullTrxName)
        Unregister a null trx
        Parameters:
        nullTrxName -