Package org.ofbiz.core.entity
Class TransactionUtil
java.lang.Object
org.ofbiz.core.entity.TransactionUtil
- All Implemented Interfaces:
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.
- Since:
- 2.0
- Version:
- $Revision: 1.5 $
- Author:
- David E. Jones
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ThreadLocal<org.ofbiz.core.entity.TransactionUtil.LocalTransaction>static final StringFields 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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanbegin()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 booleanbeginLocalTransaction(String helperName, int transactionIsolationLevel) Starts a transaction if one does not exist already.static voidA method that ensures the connection is cleared.static voidIf a connection exists in the thread local close it.static voidcommit()Commits the transaction in the current thread IF transactions are availablestatic voidcommit(boolean beganTransaction) Commits the transaction in the current thread IF transactions are available AND if beganTransaction is truestatic voidcommitLocalTransaction(boolean beganTransaction) Commits a transaction if beganTransaction is true and there is an active transaction.static ConnectionenlistConnection(XAConnection xacon) Enlists the given XAConnection and if a transaction is active in the current thread, returns a plain JDBC Connectionstatic voidenlistResource(XAResource resource) static Connectionstatic intGets the status of the transaction in the current thread IF transactions are available, otherwise returns STATUS_NO_TRANSACTIONstatic booleanChecks if there is aConnectionwith a transaction for the current thread.static voidrollback()Rolls back transaction in the current thread IF transactions are availablestatic voidrollback(boolean beganTransaction) 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 backstatic voidrollbackLocalTransaction(boolean beganTransaction) Rolls back a transaction if beganTransaction is true and there is an active transaction.static voidrollbackRequiredLocalTransaction(boolean beganTransaction) Makes a rollback the only possible outcome of the transaction in the current thread IF transactions are available.static voidMakes a roll back the only possible outcome of the transaction in the current thread IF transactions are availablestatic voidsetTransactionTimeout(int seconds) Sets the timeout of the transaction in the current thread IF transactions are available
-
Field Details
-
module
-
localTransaction
public static final ThreadLocal<org.ofbiz.core.entity.TransactionUtil.LocalTransaction> localTransaction
-
-
Constructor Details
-
TransactionUtil
public TransactionUtil()
-
-
Method Details
-
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. 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
-
getStatus
Gets the status of the transaction in the current thread IF transactions are available, otherwise returns STATUS_NO_TRANSACTION- Throws:
GenericTransactionException
-
commit
Commits the transaction in the current thread IF transactions are available AND if beganTransaction is true- Throws:
GenericTransactionException
-
commit
Commits the transaction in the current thread IF transactions are available- Throws:
GenericTransactionException
-
rollback
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
-
rollback
Rolls back transaction in the current thread IF transactions are available- Throws:
GenericTransactionException
-
setRollbackOnly
Makes a roll back the only possible outcome of the transaction in the current thread IF transactions are available- Throws:
GenericTransactionException
-
setTransactionTimeout
Sets the timeout of the transaction in the current thread IF transactions are available- Throws:
GenericTransactionException
-
enlistConnection
Enlists the given XAConnection and if a transaction is active in the current thread, returns a plain JDBC Connection- Throws:
GenericTransactionException
-
enlistResource
- Throws:
GenericTransactionException
-
beginLocalTransaction
public static boolean beginLocalTransaction(String helperName, int transactionIsolationLevel) throws GenericTransactionException Starts a transaction if one does not exist already.- Parameters:
helperName- the OfBiz helperName that is registered withinentityengine.xml. The helperName is used to retrieve the connection from the com.atlassian.core.ofbiz.CoreFactory object.transactionIsolationLevel- the transaction isolation level to set on the connection if the transaction is started, seeConnection.setTransactionIsolation(int). Negative means do not set anything. (the connection's default level will be used)- Returns:
- true if the transaction was started, false if one was active already
- Throws:
GenericTransactionException- if something goes wrong. See the getNested() method of the exception for the underlying exception.
-
getLocalTransactionConnection
- Returns:
- the
Connectionthat has an active connection for the current thread.
-
isTransactionActive
public static boolean isTransactionActive()Checks if there is aConnectionwith a transaction for the current thread.- Returns:
- true if there is an active transaction for the current thread
-
commitLocalTransaction
public static void commitLocalTransaction(boolean beganTransaction) throws GenericTransactionException Commits a transaction if beganTransaction is true and there is an active transaction. SeeisTransactionActive(). If beganTransaction is false or if there is no active transaction this method does nothing.Common usage is:
boolean started = TransactionUtil.beginLocalTransaction("default", Connection.TRANSACTION_READ_COMMITTED); ... TransactionUtil.commitLocalTransaction(started);- Parameters:
beganTransaction- whether the transaction was started- Throws:
GenericTransactionException- if something goes wrong. See the getNested() method of the exception for the underlying exception.
-
rollbackLocalTransaction
public static void rollbackLocalTransaction(boolean beganTransaction) throws GenericTransactionException Rolls back a transaction if beganTransaction is true and there is an active transaction. SeeisTransactionActive(). If beganTransaction is false or if there is no active transaction this method does nothing.Common usage is:
boolean started = TransactionUtil.beginLocalTransaction("default", Connection.TRANSACTION_READ_COMMITTED); ... TransactionUtil.rollbackLocalTransaction(started);- Parameters:
beganTransaction- whether the transaction was started- Throws:
GenericTransactionException- if something goes wrong. See the getNested() method of the exception for the underlying exception.
-
rollbackRequiredLocalTransaction
public static void rollbackRequiredLocalTransaction(boolean beganTransaction) throws GenericTransactionException Makes a rollback the only possible outcome of the transaction in the current thread IF transactions are available.- Throws:
GenericTransactionException
-
closeAndClearThreadLocalConnection
public static void closeAndClearThreadLocalConnection()If a connection exists in the thread local close it. Clear the thread local no matter what. -
clearTransactionThreadLocal
public static void clearTransactionThreadLocal()A method that ensures the connection is cleared. This is useful to call from within a servlet filter to ensure that connections are not leaked anywhere.
-