Class LocalTransaction

java.lang.Object
org.seasar.doma.jdbc.tx.LocalTransaction
Direct Known Subclasses:
KeepAliveLocalTransaction

public class LocalTransaction extends Object
A local transaction.

This instance is thread safe.

Begin a transaction by begin() or begin(TransactionIsolationLevel) and end the transaction always by commit() or rollback().

 LocalTransaction tx = DbConfig.singleton().getLocalTransaction();
 try {
     tx.begin();

     Employee employee = dao.selectById(1);
     employee.setName("SMITH");
     employee.setJobType(JobType.PRESIDENT);
     dao.update(employee);

     tx.commit();
 } finally {
     tx.rollback();
 }
 
Same instance can handle multiple transactions sequentially.
 Db
 LocalTransaction tx = DbConfig.singleton().getLocalTransaction();
 // transaction 1
 try {
     tx.begin();
     ...
     tx.commit();
 } finally {
     tx.rollback();
 }
 // transaction 2
 try {
     tx.begin();
     ...
     tx.commit();
 } finally {
     tx.rollback();
 }
 
Any exceptions that are thrown from this class methods roll back the transaction.
  • Field Details

  • Constructor Details

  • Method Details

    • begin

      public void begin()
      Begin this transaction.
      Throws:
      TransactionAlreadyBegunException - if this transaction is already begun
      JdbcException - if a JDBC related error occurs
    • begin

      public void begin(TransactionIsolationLevel transactionIsolationLevel)
      Begin this transaction with the specified transaction isolation level.
      Parameters:
      transactionIsolationLevel - the transaction isolation level
      Throws:
      DomaNullPointerException - if the transactionIsolationLevel is null
      TransactionAlreadyBegunException - if this transaction is already begun
      JdbcException - if a JDBC related error occurs
    • beginInternal

      protected void beginInternal(TransactionIsolationLevel transactionIsolationLevel, String callerMethodName)
    • getLocalTransactionContext

      protected LocalTransactionContext getLocalTransactionContext()
    • commit

      public void commit()
      Commits this transaction.
      Throws:
      TransactionNotYetBegunException - if this transaction is not yet begun
      JdbcException - if a JDBC related error occurs
    • suspend

      public LocalTransactionContext suspend()
      Suspends this transaction.
      Returns:
      the transaction context that is required to resume this transaction
      Throws:
      TransactionNotYetBegunException - if this transaction is not yet begun
    • resume

      public void resume(LocalTransactionContext context)
      Resumes this transaction.

      This method does not throw any exceptions.

      Parameters:
      context - the transaction context that is returned from suspend()
    • rollback

      public void rollback()
      Undoes all changes made in this transaction.

      If this transaction is not begun, this method does nothing.

      This method does not throw any exceptions.

    • rollbackInternal

      protected void rollbackInternal(String callerMethodName)
    • setSavepoint

      public void setSavepoint(String savepointName)
      Creates a save point with the specified name.
      Parameters:
      savepointName - the name of the save point
      Throws:
      DomaNullPointerException - if the savepointName is null
      TransactionNotYetBegunException - if this transaction is not yet begun
      SavepointAlreadyExistsException - if the save point already exists
      JdbcException - if a JDBC related error occurs
    • hasSavepoint

      public boolean hasSavepoint(String savepointName)
      Whether this transaction has the specified save point.
      Parameters:
      savepointName - the name of the save point
      Returns:
      true if this transaction has the save point
      Throws:
      DomaNullPointerException - if the savepointName is null
      TransactionNotYetBegunException - if this transaction is not yet begun
    • releaseSavepoint

      public void releaseSavepoint(String savepointName)
      Removes the specified save point and subsequent save points from this transaction.
      Parameters:
      savepointName - the name of the save point
      Throws:
      DomaNullPointerException - if the savepointName is null
      TransactionNotYetBegunException - if this transaction is not yet begun
      JdbcException - if a JDBC related error occurs
    • rollback

      public void rollback(String savepointName)
      Undoes all changes made after the given save point.
      Parameters:
      savepointName - the name of the save point
      Throws:
      DomaNullPointerException - if the savepointName is null
      SavepointNotFoundException - if the save point is not found
      TransactionNotYetBegunException - if this transaction is not yet begun
      JdbcException - if a JDBC related error occurs
    • end

      protected void end(String callerMethodName)
    • endInternal

      protected void endInternal(LocalTransactionContext context, String callerMethodName)
    • release

      protected void release(LocalTransactionContext context, String callerMethodName)
      Releases the transaction context.

      This method does not throw any exceptions.

      Parameters:
      context - the transaction context
      callerMethodName - the caller method name
    • closeConnection

      protected void closeConnection(Connection connection)
    • toString

      public String toString()
      Returns an unique string to identify this transaction.
      Overrides:
      toString in class Object
    • isActive

      public boolean isActive()
      Whether this transaction is active.
      Returns:
      true if this transaction is active
    • isActiveInternal

      protected boolean isActiveInternal(LocalTransactionContext context)
    • setRollbackOnly

      public void setRollbackOnly()
      Marks this transaction to undo in the end of the transaction.
    • isRollbackOnly

      public boolean isRollbackOnly()
      Whether this transaction is marked to be undone.
      Returns:
      true if the current transaction is marked.