com.ibatis.sqlmap.client
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.

另请参见:
SqlMapSession, SqlMapClient

方法摘要
 void commitTransaction()
          Commits the currently started transaction.
 void endTransaction()
          Ends a transaction and rolls back if necessary.
 java.sql.Connection getCurrentConnection()
          Returns the current connection in use.
 javax.sql.DataSource getDataSource()
          Returns the DataSource instance currently being used by the SqlMapSession.
 java.sql.Connection getUserConnection()
          Deprecated. Use getCurrentConnection() instead.
 void setUserConnection(java.sql.Connection connnection)
          Allows the developer to easily use an externally supplied connection when executing statements.
 void startTransaction()
          Demarcates the beginning of a transaction scope.
 void startTransaction(int transactionIsolation)
          Demarcates the beginning of a transaction scope using the specified transaction isolation.
 

方法详细信息

startTransaction

void startTransaction()
                      throws java.sql.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.

抛出异常:
java.sql.SQLException - If an error occurs while starting the transaction, or the transaction could not be started.

startTransaction

void startTransaction(int transactionIsolation)
                      throws java.sql.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.

抛出异常:
java.sql.SQLException - If an error occurs while starting the transaction, or the transaction could not be started.

commitTransaction

void commitTransaction()
                       throws java.sql.SQLException
Commits the currently started transaction.

抛出异常:
java.sql.SQLException - If an error occurs while committing the transaction, or the transaction could not be committed.

endTransaction

void endTransaction()
                    throws java.sql.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().

抛出异常:
java.sql.SQLException - If an error occurs during rollback or the transaction could not be ended.

setUserConnection

void setUserConnection(java.sql.Connection connnection)
                       throws java.sql.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
   }
 }
 

参数:
connnection -
抛出异常:
java.sql.SQLException

getUserConnection

java.sql.Connection getUserConnection()
                                      throws java.sql.SQLException
Deprecated. Use getCurrentConnection() instead.

Returns the current user supplied connection as set by setUserConnection().

TODO : DEPRECATED

返回:
The current user supplied connection.
抛出异常:
java.sql.SQLException

getCurrentConnection

java.sql.Connection getCurrentConnection()
                                         throws java.sql.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.

返回:
The current connection or null.
抛出异常:
java.sql.SQLException

getDataSource

javax.sql.DataSource getDataSource()
Returns the DataSource instance currently being used by the SqlMapSession.

返回:
The DataSource instance currently being used by the SqlMapSession.