Interface SqlMapTransactionManager

All Known Subinterfaces:
ExtendedSqlMapClient, SqlMapClient, SqlMapSession
All Known Implementing Classes:
SqlMapClientImpl, SqlMapSessionImpl

public interface SqlMapTransactionManager
This interface declares methods for demarcating SQL Map transactions.
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Commits the currently started transaction.
    void
    Ends a transaction and rolls back if necessary.
    Returns the current connection in use.
    Returns the DataSource instance currently being used by the SqlMapSession.
    Deprecated.
    Use getCurrentConnection() instead.
    void
    Allows the developer to easily use an externally supplied connection when executing statements.
    void
    Demarcates the beginning of a transaction scope.
    void
    startTransaction(int transactionIsolation)
    Demarcates the beginning of a transaction scope using the specified transaction isolation.
  • Method Details

    • startTransaction

      void startTransaction() throws SQLException
      Demarcates the beginning of a transaction scope. Transactions must be properly committed or rolled back to be effective. Use the following pattern when working with transactions:
       try {
         sqlMap.startTransaction();
         // do work
         sqlMap.commitTransaction();
       } finally {
         sqlMap.endTransaction();
       }
       

      Always call endTransaction() once startTransaction() has been called.

      Throws:
      SQLException - the SQL exception
    • startTransaction

      void startTransaction(int transactionIsolation) throws SQLException
      Demarcates the beginning of a transaction scope using the specified transaction isolation. Transactions must be properly committed or rolled back to be effective. Use the following pattern when working with transactions:
       try {
         sqlMap.startTransaction(Connection.TRANSACTION_REPEATABLE_READ);
         // do work
         sqlMap.commitTransaction();
       } finally {
         sqlMap.endTransaction();
       }
       

      Always call endTransaction() once startTransaction() has been called.

      Parameters:
      transactionIsolation - the transaction isolation
      Throws:
      SQLException - the SQL exception
    • commitTransaction

      void commitTransaction() throws SQLException
      Commits the currently started transaction.
      Throws:
      SQLException - If an error occurs while committing the transaction, or the transaction could not be committed.
    • endTransaction

      void endTransaction() throws SQLException
      Ends a transaction and rolls back if necessary. If the transaction has been started, but not committed, it will be rolled back upon calling endTransaction().
      Throws:
      SQLException - If an error occurs during rollback or the transaction could not be ended.
    • setUserConnection

      void setUserConnection(Connection connnection) throws SQLException
      Allows the developer to easily use an externally supplied connection when executing statements.

      Important: Using a user supplied connection basically sidesteps the transaction manager, so you are responsible for appropriately. Here's a (very) simple example (throws SQLException):

       try {
         Connection connection = dataSource.getConnection();
         sqlMap.setUserConnection(connection);
         // do work
         connection.commit();
       } catch (SQLException e) {
         try {
           if (connection != null)
             commit.rollback();
         } catch (SQLException ignored) {
           // generally ignored
         }
         throw e; // rethrow the exception
       } finally {
         try {
           if (connection != null)
             connection.close();
         } catch (SQLException ignored) {
           // generally ignored
         }
       }
       
      Parameters:
      connnection - the new user connection
      Throws:
      SQLException - the SQL exception
    • getUserConnection

      Connection getUserConnection() throws SQLException
      Deprecated.
      Use getCurrentConnection() instead.
      Returns the current user supplied connection as set by setUserConnection().

      TODO : DEPRECATED

      Returns:
      The current user supplied connection.
      Throws:
      SQLException - the SQL exception
    • getCurrentConnection

      Connection getCurrentConnection() throws SQLException
      Returns the current connection in use. If no connection exists null will be returned. There may be no connection if no transaction has been started, and if no user provided connection has been set.
      Returns:
      The current connection or null.
      Throws:
      SQLException - the SQL exception
    • getDataSource

      DataSource getDataSource()
      Returns the DataSource instance currently being used by the SqlMapSession.
      Returns:
      The DataSource instance currently being used by the SqlMapSession.