Search This Blog

Wednesday 29 August 2012

Spring and Database - the cool stuff - 2

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 :
  1. Acquire a database connection
  2. Open a transaction
  3. Perform the actual database operation
  4. Commit/Rollback the transaction
  5. Close/Release the resources
 In the above operation only step 3 changes. In all our different methods the remaining steps are mostly repeat code. It only changes depending in the technique we use to access the data. If it is JDBC, the resource is a connection, if Hibernate the resource is a sessionFactory. But we are still repeating the code across the entire project and then the next.
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
We can directly wire these templates in our code and use them :
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
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
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