Class TransactionUtil

java.lang.Object
org.ofbiz.core.entity.TransactionUtil
All Implemented Interfaces:
javax.transaction.Status

public class TransactionUtil extends Object implements 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

    Fields
    Modifier and Type
    Field
    Description
    static final ThreadLocal<org.ofbiz.core.entity.TransactionUtil.LocalTransaction>
     
    static final String
     

    Fields 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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    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
    A method that ensures the connection is cleared.
    static void
    If a connection exists in the thread local close it.
    static void
    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
    Enlists the given XAConnection and if a transaction is active in the current thread, returns a plain JDBC Connection
    static void
     
    static Connection
     
    static int
    Gets the status of the transaction in the current thread IF transactions are available, otherwise returns STATUS_NO_TRANSACTION
    static boolean
    Checks if there is a Connection with a transaction for the current thread.
    static void
    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
    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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • module

      public static final String module
    • localTransaction

      public static final ThreadLocal<org.ofbiz.core.entity.TransactionUtil.LocalTransaction> localTransaction
  • Constructor Details

    • TransactionUtil

      public TransactionUtil()
  • Method Details

    • begin

      public static boolean begin() throws GenericTransactionException
      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

      public static int getStatus() throws GenericTransactionException
      Gets the status of the transaction in the current thread IF transactions are available, otherwise returns STATUS_NO_TRANSACTION
      Throws:
      GenericTransactionException
    • commit

      public static void commit(boolean beganTransaction) throws GenericTransactionException
      Commits the transaction in the current thread IF transactions are available AND if beganTransaction is true
      Throws:
      GenericTransactionException
    • commit

      public static void commit() throws GenericTransactionException
      Commits the transaction in the current thread IF transactions are available
      Throws:
      GenericTransactionException
    • rollback

      public static void rollback(boolean beganTransaction) throws GenericTransactionException
      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

      public static void rollback() throws GenericTransactionException
      Rolls back transaction in the current thread IF transactions are available
      Throws:
      GenericTransactionException
    • setRollbackOnly

      public static void setRollbackOnly() throws GenericTransactionException
      Makes a roll back the only possible outcome of the transaction in the current thread IF transactions are available
      Throws:
      GenericTransactionException
    • setTransactionTimeout

      public static void setTransactionTimeout(int seconds) throws GenericTransactionException
      Sets the timeout of the transaction in the current thread IF transactions are available
      Throws:
      GenericTransactionException
    • enlistConnection

      public static Connection enlistConnection(XAConnection xacon) throws GenericTransactionException
      Enlists the given XAConnection and if a transaction is active in the current thread, returns a plain JDBC Connection
      Throws:
      GenericTransactionException
    • enlistResource

      public static void enlistResource(XAResource resource) throws GenericTransactionException
      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 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)
      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

      public static Connection getLocalTransactionConnection()
      Returns:
      the Connection that has an active connection for the current thread.
    • isTransactionActive

      public static boolean isTransactionActive()
      Checks if there is a Connection with 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. See 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);
       
      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. See 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);
       
      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.