DBCP
: Use DBCP when you want to use Apache DBCP for connection management. Please see the DAO online guide for information on how to configure the DBCP connection pool.JNDI
: When you want to use the application server's implementation of the connection pool, all you have to do is provide the JNDI name of your connection pool and the DAO framework will use it for getting a connection.<transactionManager type="JDBC"> <property name="DataSource" value="JNDI"/> <property name="DBJndiContext" value="java:comp/env/jdbc/MyDataSource"/> </transactionManager>
Then you will have to create a class that extends JdbcDaoTemplate.java to implement your business interface. In our sample, we created JDBCContactDAO.java. In the business method, you can ask your superclass for the connection by calling getConnection()
. Since we are not using any persistence framework, we'll have to create our own SQL
queries and execute them.
public int updateContact(Contact contact) {
try {
Connection conn = getConnection();
PreparedStatement updateStmt =
conn.prepareStatement("UPDATE DB2ADMIN.CONTACT
SET FIRSTNAME=?,LASTNAME=? WHERE CONTACTID=?");
updateStmt.setString(1, contact.getFirstName());
updateStmt.setString(2, contact.getLastName());
updateStmt.setInt(3, contact.getContactId());
return updateStmt.executeUpdate();
} catch (SQLException ex) {
throw new DaoException(ex);
}
}
When using a JDBC transactionManager
, the DAO framework will control the transaction by calling commit
and rollback
methods on the Connection
object, so transactions will be handled at the Connection
level instead of participating in global transaction.
JTA
If you are creating a J2EE application, then it is a much better idea to use the connection pool provided by your application server, because it will perform much better than a SIMPLE
or DBCP
connection pool. Also, with a J2EE application, RDBMS will be only one of the transactional sources; in addition to RDBMS, you will also have other things like JCA, MQ Server, etc. This means you cannot start and commit transactions at the connection level; instead, your code should participate in global transaction by calling the begin()
and commit()
methods on a UserTransaction
. For this type of requirement, you can use JTA as transctionManager
and provide the JNDI URL of both your DataSource
pool and UserTransaction
object to it.
<transactionManager type="JTA">
<property name="DBJndiContext"
value="java:comp/env/jdbc/MyDataSource"/>
<property name="UserTransaction"
value="java:comp/env/UserTransaction"/>
</transactionManager>
Hibernate
Since Hibernate is a very popular persistence framework, iBatis DAO offers support for it. To use Hibernate in your application, add a <transactionManager>
element in your DAOMap.xml file, as follows.
<transactionManager type="HIBERNATE">
<property name="hibernate.dialect"
value="net.sf.hibernate.dialect.Cloudscape"/>
<property name="hibernate.connection.driver_class"
value="com.ibm.db2j.jdbc.DB2jDriver"/>
<property name="hibernate.connection.url"
value="jdbc:db2j:D:/cloudscape/wpsdb"/>
<property name="hibernate.connection.username"
value="db2admin/>
<property name="hibernate.connection.password"
value="db2admin"/>
<property name="class.1"
value="com.sample.contact.Contact"/>
</transactionManager>
You also need to create a DAO class extending HibernateDaoTemplate
. Inside your DAO, you can access the Hibernate Session
object by calling the getSession()
method.
SQL Map
Please look at the sample application (in the Resources section) for details about how to use the SQL Map persistence framework in your application.
External
An external transaction manager allows transactions to be externally controlled by the DAO framework. This behavior is good for interacting with non-RDBMS data sources. In the next section, we will see how to use the DAO framework to use an XML file as a data source.
<transactionManager type="EXTERNAL">
</transactionManager>