public class TransactionUtil extends Object implements 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.
| Modifier and Type | Field and Description |
|---|---|
static ThreadLocal<org.ofbiz.core.entity.TransactionUtil.LocalTransaction> |
localTransaction |
static String |
module |
STATUS_ACTIVE, STATUS_COMMITTED, STATUS_COMMITTING, STATUS_MARKED_ROLLBACK, STATUS_NO_TRANSACTION, STATUS_PREPARED, STATUS_PREPARING, STATUS_ROLLEDBACK, STATUS_ROLLING_BACK, STATUS_UNKNOWN| Constructor and Description |
|---|
TransactionUtil() |
| Modifier and Type | Method and 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 |
beginLocalTransaction(String helperName,
int transactionIsolationLevel)
Starts a transaction if one does not exist already.
|
static void |
clearTransactionThreadLocal()
A method that ensures the connection is cleared.
|
static void |
closeAndClearThreadLocalConnection()
If a connection exists in the thread local close it.
|
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 void |
commitLocalTransaction(boolean beganTransaction)
Commits a transaction if beganTransaction is true and there is an active transaction.
|
static Connection |
enlistConnection(XAConnection xacon)
Enlists the given XAConnection and if a transaction is active in the current thread, returns a plain JDBC Connection
|
static void |
enlistResource(XAResource resource) |
static Connection |
getLocalTransactionConnection() |
static int |
getStatus()
Gets the status of the transaction in the current thread IF
transactions are available, otherwise returns STATUS_NO_TRANSACTION
|
static boolean |
isTransactionActive()
Checks if there is a
Connection with a transaction for the current thread. |
static void |
rollback()
Rolls back transaction in the current thread IF transactions are available
|
static void |
rollback(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 back
|
static void |
rollbackLocalTransaction(boolean beganTransaction)
Rolls back a transaction if beganTransaction is true and there is an active transaction.
|
static void |
rollbackRequiredLocalTransaction(boolean beganTransaction)
Makes a rollback the only possible outcome of the transaction in the current thread IF transactions are available.
|
static void |
setRollbackOnly()
Makes a roll back 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
|
public static final String module
public static final ThreadLocal<org.ofbiz.core.entity.TransactionUtil.LocalTransaction> localTransaction
public static boolean begin()
throws GenericTransactionException
GenericTransactionExceptionpublic static int getStatus()
throws GenericTransactionException
GenericTransactionExceptionpublic static void commit(boolean beganTransaction)
throws GenericTransactionException
GenericTransactionExceptionpublic static void commit()
throws GenericTransactionException
GenericTransactionExceptionpublic static void rollback(boolean beganTransaction)
throws GenericTransactionException
GenericTransactionExceptionpublic static void rollback()
throws GenericTransactionException
GenericTransactionExceptionpublic static void setRollbackOnly()
throws GenericTransactionException
GenericTransactionExceptionpublic static void setTransactionTimeout(int seconds)
throws GenericTransactionException
GenericTransactionExceptionpublic static Connection enlistConnection(XAConnection xacon) throws GenericTransactionException
GenericTransactionExceptionpublic static void enlistResource(XAResource resource) throws GenericTransactionException
GenericTransactionExceptionpublic static boolean beginLocalTransaction(String helperName, int transactionIsolationLevel) throws GenericTransactionException
helperName - the OfBiz helperName that is registered within
entityengine.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, see
Connection.setTransactionIsolation(int). Negative means do not set anything.
(the connection's default level will be used)GenericTransactionException - if something goes wrong. See the
getNested() method of the exception for the underlying exception.public static Connection getLocalTransactionConnection()
Connection that has an active connection for the current thread.public static boolean isTransactionActive()
Connection with a transaction for the current thread.public static void commitLocalTransaction(boolean beganTransaction)
throws GenericTransactionException
isTransactionActive().
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);
beganTransaction - whether the transaction was startedGenericTransactionException - if something goes wrong. See the getNested() method of the exception
for the underlying exception.public static void rollbackLocalTransaction(boolean beganTransaction)
throws GenericTransactionException
isTransactionActive().
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);
beganTransaction - whether the transaction was startedGenericTransactionException - if something goes wrong. See the getNested() method of the exception
for the underlying exception.public static void rollbackRequiredLocalTransaction(boolean beganTransaction)
throws GenericTransactionException
GenericTransactionExceptionpublic static void closeAndClearThreadLocalConnection()
public static void clearTransactionThreadLocal()
Copyright © 2018 Atlassian. All rights reserved.