com.ibatis.dao.client
Interface DaoManager


public interface DaoManager

This interface describes the DaoManager interface. It provides access to all DAOs it manages and also allows transactions to be committed and ended (possibly rolled back).

DAO instances returned from the DAO Manager are proxied such that transactions can be automatically started, committed and ended (or rolled back). This is a similar semantic to the JDBC autocommit, but much more powerful.

Alternatively, tranasctions can be controlled programmatically, allowing you to demarcate wider scope transactions as needed.

Either way, transactions will only be started for those contexts that require them. No unneccessary transactions will be started. When commitTransaction() and endTransaction() are called, all transactions which have been started in each configured context will be committed and ended respectively. endTransaction() will automatically rollback any transactions that have not been committed.

Here's a couple of examples:

 

// ************************************************************** // AUTO COMMIT TRANASACTION SEMANTIC // **************************************************************

DaoManager daoManager = DaoManagerBuilder.buildDaoManager(reader); PersonDao personDao = daoManager.getDao(PersonDao.class); // A transaction will be automatically started committed and ended // by calling any method on the DAO. The following insert and update // are TWO separate transactions. personDao.insertPerson (person); // Starts transaction person.setLastName("Begin"); personDao.updatePerson (person); // Starts a new transaction

// ************************************************************** // PROGRAMMATIC DEMARCATION OF TRANSACTION SCOPE // **************************************************************

DaoManager daoManager = DaoManagerBuilder.buildDaoManager(reader); PersonDao personDao = daoManager.getDao(PersonDao.class); try { // Calling startTransaction() tells the DAO Manager that you // are going to be managing the transactions manually. daoManager.startTransaction(); personDao.insertPerson (person); person.setLastName("Begin"); personDao.updatePerson (person); // Commit all active transactions in all contexts daoManager.commitTransaction(); } finally { // End all active transactions in all contexts and rollback if necessary. daoManager.endTransaction(); }

Important: In order to achieve global transaction behaviour (i.e. two phase commit), you'll need to configure all of your contexts using JTA, JNDI and XA compliant DataSources.


Method Summary
 void commitTransaction()
          Commits all transactions currently started for all DAO contexts managed by this DaoManager.
 void endTransaction()
          Ends all transactions currently started for all DAO contexts managed by this DaoManager.
 Dao getDao(java.lang.Class type)
          Gets a Dao instance for the requested interface type.
 Dao getDao(java.lang.Class iface, java.lang.String contextId)
          Gets a Dao instance for the requested interface type registered under the context with the specified id.
 DaoTransaction getTransaction(Dao dao)
          Gets the transaction that the provided Dao is currently working under.
 void startTransaction()
          Starts a transaction scope managed by this DaoManager.
 

Method Detail

getDao

public Dao getDao(java.lang.Class type)
Gets a Dao instance for the requested interface type.

Parameters:
type - The interface or generic type for which an implementation should be returned.
Returns:
The Dao implementation instance.

getDao

public Dao getDao(java.lang.Class iface,
                  java.lang.String contextId)
Gets a Dao instance for the requested interface type registered under the context with the specified id.

Parameters:
iface - The interface or generic type for which an implementation should be returned.
contextId - The ID of the context under which to find the DAO implementation (use for multiple interface defs).
Returns:
The Dao implementation instance.

getTransaction

public DaoTransaction getTransaction(Dao dao)
Gets the transaction that the provided Dao is currently working under. If there is no current transaction in scope, one will be started.

Parameters:
dao - The Dao to find a transaction for.
Returns:
The Transaction under which the Dao provided is working under.

startTransaction

public void startTransaction()
Starts a transaction scope managed by this DaoManager. If this method isn't called, then all DAO methods use "autocommit" semantics.


commitTransaction

public void commitTransaction()
Commits all transactions currently started for all DAO contexts managed by this DaoManager.


endTransaction

public void endTransaction()
Ends all transactions currently started for all DAO contexts managed by this DaoManager. If any transactions have not been successfully committed, then those remaining will be rolled back.