Package org.compiere.util
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/
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addTrxEventListener(TrxEventListener listener)
boolean
close()
End Transaction and Close Connectionboolean
commit()
Commitboolean
commit(boolean throwException)
Commitstatic String
createTrxName()
Create unique Transaction Namestatic String
createTrxName(String prefix)
Create unique Transaction Nameprotected void
finalize()
static Trx
get(String trxName, boolean createNew)
Get Transactionstatic Trx
get(String trxName, boolean createNew, Connection con)
Get Transaction in a Connectionstatic Trx[]
getActiveTransactions()
Connection
getConnection()
Get connectionConnection
getConnection(boolean createNew)
Get ConnectionString
getDisplayName()
Savepoint
getLastWFSavepoint()
Date
getStartTime()
String
getStrackTrace()
int
getTimeout()
String
getTrxName()
Get Nameboolean
hasChangesMadeByEventListener()
boolean
isActive()
Transaction is Activevoid
releaseSavepoint(Savepoint savepoint)
Release Savepointboolean
removeTrxEventListener(TrxEventListener listener)
boolean
rollback()
Rollbackboolean
rollback(boolean throwException)
Rollbackboolean
rollback(Savepoint savepoint)
Rollbackboolean
rollbackAndCloseOnTimeout()
Rollback and End Transaction, Close Connection and Throws an Exceptionstatic void
run(String trxName, TrxRunnable r)
Execute runnable object using provided transaction.static void
run(TrxRunnable r)
void
setChangesMadeByEventListener(boolean changesMade)
Indicate additional db changes have been made by event listenervoid
setDisplayName(String displayName)
void
setLastWFSavepoint(Savepoint savepoint)
Savepoint
setSavepoint(String name)
void
setTimeout(int timeout)
set transaction timeout ( in seconds )boolean
start()
Start Trxstatic void
startTrxMonitor()
String
toString()
String Representation
-
-
-
Field Detail
-
trace
protected Exception trace
-
-
Method Detail
-
get
public static Trx get(String trxName, boolean createNew)
Get Transaction- Parameters:
trxName
- trx namecreateNew
- 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 namecreateNew
- if false, null is returned if not foundcon
- 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
-
setSavepoint
public Savepoint setSavepoint(String name) throws SQLException
- Parameters:
name
-- Returns:
- Savepoint
- Throws:
SQLException
-
setLastWFSavepoint
public void setLastWFSavepoint(Savepoint savepoint)
-
getLastWFSavepoint
public Savepoint getLastWFSavepoint()
-
releaseSavepoint
public void releaseSavepoint(Savepoint savepoint) throws SQLException
Release Savepoint- Parameters:
savepoint
-- Throws:
SQLException
-
toString
public String toString()
String Representation
-
getActiveTransactions
public static Trx[] getActiveTransactions()
- Returns:
- Trx[]
-
run
public static void run(TrxRunnable r)
- See Also:
run(String, TrxRunnable)
-
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
- orAdempiereException
-
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
-
-