In the previous post we saw that Spring gave us a generic way to handle Data access exceptions. The next goodie from the Spring box is the ability to ignore the database access code and focus on the actual database logic.
The code to perform any database operation involves :
Spring uses the template method design pattern to help make our life simpler.
The implementation can be broken into two parts - templates and callbacks. The templates deal with the fixed part -> acquiring connections, cleaning up, committing transactions etc.
Step 3 is the varying part of the process. The one that changes with every method. The one where we write the actual logic - the query, the update code etc. This is implemented in the callbacks. When step 3 is to be executed, the template will delegate control to the callback.
There are a variety of templates provided by Spring based on the technology being used. I copied the below table from the Spring in Action book:
We can directly wire these templates in our code and use them :
These support classes provide some convenience methods. They also include a reference to the appropriate template. So if in the above example our PersonDAO class extended JdbcDaoSupport, then the above code wouldn't be necessary.
The support class also provides us access to the resource exposed by the data access technology and internally used by the template.For example consider a fragment of the below class
The code to perform any database operation involves :
- Acquire a database connection
- Open a transaction
- Perform the actual database operation
- Commit/Rollback the transaction
- Close/Release the resources
Spring uses the template method design pattern to help make our life simpler.
The implementation can be broken into two parts - templates and callbacks. The templates deal with the fixed part -> acquiring connections, cleaning up, committing transactions etc.
Step 3 is the varying part of the process. The one that changes with every method. The one where we write the actual logic - the query, the update code etc. This is implemented in the callbacks. When step 3 is to be executed, the template will delegate control to the callback.
There are a variety of templates provided by Spring based on the technology being used. I copied the below table from the Spring in Action book:
Template | Data Access Technology |
---|---|
org.springframework.jdbc.core.JdbcTemplate | JDBC connections |
org.springframework.jca.cci.core.CciTemplate | JCA CCI connections |
org.springframework.jdbc.core.namedparam. NamedParameterJdbcTemplate | JDBC connections with support for named parameters |
org.springframework.jdbc.core.simple. SimpleJdbcTemplate | JDBC connections, simplified with Java 5 constructs |
org.springframework.orm.hibernate. HibernateTemplate | Hibernate 2.x sessions |
org.springframework.orm.hibernate3. HibernateTemplate | Hibernate 3.x sessions |
org.springframework.orm.ibatis. SqlMapClientTemplate | iBATIS SqlMap clients |
org.springframework.orm.jdo.JdoTemplate | Java Data Object implementations |
org.springframework.orm.jpa.JpaTemplate | Java Persistence API entity managers |
public class PersonDAO implements IPersonDAO { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } }To simply the usage of these templates Spring also provides a series of DAO support classes.
Template | Data Access Technology |
---|---|
org.springframework.jdbc.core.support. JdbcDaoSupport | JDBC connections |
org.springframework.jca.cci.support. CciDaoSupport | JCA CCI connections |
org.springframework.jdbc.core.namedparam. NamedParameterJdbcDaoSupport | JDBC connections with support for named parameters |
org.springframework.jdbc.core.simple. SimpleJdbcDaoSupport | JDBC connections, simplified with Java 5 constructs |
org.springframework.orm.hibernate. support.HibernateDaoSupport | Hibernate 2.x sessions |
org.springframework.orm.hibernate3. support.HibernateDaoSupport | Hibernate 3.x sessions |
org.springframework.orm.ibatis.support. SqlMapClientDaoSupport | iBATIS SqlMap clients |
org.springframework.orm.jdo.support. JdoDaoSupport | Java Data Object implementations |
org.springframework.orm.jpa. support.JpaDaoSupport | Java Persistence API entity managers |
The support class also provides us access to the resource exposed by the data access technology and internally used by the template.For example consider a fragment of the below class
public abstract class HibernateDaoSupport extends DaoSupport { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } protected final Session getSession() throws DataAccessResourceFailureException, IllegalStateException { return getSession(this.hibernateTemplate.isAllowCreate()); } }The class provides us with a clean technique to get access to the session object for Hibernate. Similar for JDBC, the JdbcDaoSupportclass provides us with a technique to get the connection resource.
No comments:
Post a Comment