Class 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/
    • Method Detail

      • get

        public static Trx get​(String trxName,
                              boolean createNew)
        Get Transaction
        Parameters:
        trxName - trx name
        createNew - if false, null is returned if not found
        Returns:
        Transaction or null
      • get

        public static Trx get​(String trxName,
                              boolean createNew,
                              Connection con)
        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()
      • createTrxName

        public static String createTrxName​(String prefix)
        Create unique Transaction Name
        Parameters:
        prefix - optional prefix
        Returns:
        unique 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 Connection
        Parameters:
        createNew - if true, create new connection if the trx does not have one created yet
        Returns:
        connection
      • getTrxName

        public String getTrxName()
        Get Name
        Returns:
        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()
        Transaction is Active
        Returns:
        true if transaction active
      • rollback

        public boolean rollback​(boolean throwException)
                         throws SQLException
        Rollback
        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
        Returns:
        true if success, false if failed or transaction already rollback
      • rollback

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

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

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

        public boolean rollbackAndCloseOnTimeout()
        Rollback and End Transaction, Close Connection and Throws an Exception
        Returns:
        true if success
      • close

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

        public void setLastWFSavepoint​(Savepoint savepoint)
      • getLastWFSavepoint

        public Savepoint getLastWFSavepoint()
      • toString

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

        public static Trx[] getActiveTransactions()
        Returns:
        Trx[]
      • 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()
        Returns:
        trx timoue value in second
      • setTimeout

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

        public void addTrxEventListener​(TrxEventListener listener)
        Parameters:
        listener -
      • removeTrxEventListener

        public boolean removeTrxEventListener​(TrxEventListener listener)
      • getStrackTrace

        public String getStrackTrace()
      • getDisplayName

        public String getDisplayName()
      • setDisplayName

        public void setDisplayName​(String displayName)
      • setChangesMadeByEventListener

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

        public boolean hasChangesMadeByEventListener()
        Returns:
        true if event listener(s) has flag that additional db changes have been made