Class SQLTransaction

java.lang.Object
com.landawn.abacus.jdbc.SQLTransaction
All Implemented Interfaces:
Transaction, Closeable, AutoCloseable

public final class SQLTransaction extends Object implements Transaction, Closeable
DO NOT CLOSE the connection manually. It will be automatically closed after the transaction is committed or rolled back.
Since:
0.8
Author:
Haiyang Li
  • Method Details

    • id

      public String id()
      Description copied from interface: Transaction
      Returns the identifier of this transaction if it's supported.
      Specified by:
      id in interface Transaction
      Returns:
    • connection

      public Connection connection()
      Returns:
    • isolationLevel

      public IsolationLevel isolationLevel()
      Specified by:
      isolationLevel in interface Transaction
      Returns:
    • status

      public Transaction.Status status()
      Specified by:
      status in interface Transaction
      Returns:
    • isActive

      public boolean isActive()
      Checks if is active.
      Specified by:
      isActive in interface Transaction
      Returns:
      true, if is active
    • commit

      public void commit() throws com.landawn.abacus.exception.UncheckedSQLException
      Specified by:
      commit in interface Transaction
      Throws:
      com.landawn.abacus.exception.UncheckedSQLException - the unchecked SQL exception
    • rollback

      @Deprecated public void rollback() throws com.landawn.abacus.exception.UncheckedSQLException
      Deprecated.
      replaced by #rollbackIfNotCommitted()
      Transaction can be started:
       
         final SQLTransaction tran = sqlExecutor.beginTransaction(IsolationLevel.READ_COMMITTED);
         try {
             // sqlExecutor.insert(...);
             // sqlExecutor.update(...);
             // sqlExecutor.query(...);
      
             tran.commit();
         } finally {
             // The connection will be automatically closed after the transaction is committed or rolled back.
             tran.rollbackIfNotCommitted();
         }
       
       
      Specified by:
      rollback in interface Transaction
      Throws:
      com.landawn.abacus.exception.UncheckedSQLException - the unchecked SQL exception
      See Also:
      • SQLExecutor#beginTransaction(IsolationLevel)
    • rollbackIfNotCommitted

      public void rollbackIfNotCommitted() throws com.landawn.abacus.exception.UncheckedSQLException
      Rollback if not committed.
      Throws:
      com.landawn.abacus.exception.UncheckedSQLException - the unchecked SQL exception
    • runNotInMe

      public <E extends Throwable> void runNotInMe(com.landawn.abacus.util.Throwables.Runnable<E> cmd) throws E
      Execute the specified Runnable not this transaction.
      Type Parameters:
      E -
      Parameters:
      cmd -
      Throws:
      E
    • callNotInMe

      public <R, E extends Throwable> R callNotInMe(com.landawn.abacus.util.Throwables.Callable<R,E> cmd) throws E
      Execute the specified Callable not this transaction.
      Type Parameters:
      R -
      E -
      Parameters:
      cmd -
      Returns:
      Throws:
      E
    • close

      public void close()
      rollbackIfNotCommitted is called.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      See Also:
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
      Returns:
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
      Parameters:
      obj -
      Returns:
      true, if successful
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Returns: