Tuesday, June 1, 2010

HibernateDaoSupport with HibernateTemplate - defaultAutoCommit set false...

I am working on a web application which needs hibernate for DB integration. So i decided to use hibernate with spring. As we all know that Spring is very lightweight and supports hibernate in an easy way. Also i am used to spring JdbcDaoSupport for DB integration and StoredProcedure class for SQL Server stored procedures.

While working this time i decided to use HibernateDaoSupport. Your DAO class must extend this class to use HibernateTemplate class.

Steps to make DAO using this,

- Extend HibernateDaoSupport and also an interface if you have.
- Define your methods which provides different Database operations (CREATE, DELETE, FETCH, UPDATE, INSERT etc...)
- Define required beans as below,

 <bean id="dataSource"  
 class="org.apache.commons.dbcp.BasicDataSource">  
  <property name="driverClassName">  
   <value>com.mysql.jdbc.Driver</value>  
  </property>  
  <property name="url">  
   <value>jdbc:mysql://localhost/testdb</value>  
  </property>  
  <property name="username" value="username" />  
  <property name="password" value="passwordforit" />  
  <property name="defaultAutoCommit" value="true" />  
  <property name="defaultTransactionIsolation" value="2" />  
  <property name="maxActive" value="100" />  
  <property name="maxIdle" value="50" />  
  <property name="minIdle" value="50" />  
  <property name="maxWait" value="100" />  
  <property name="initialSize" value="20" />  
 </bean>  
 <!-- Database Property -->  
 <bean id="hibernateProperties"  
 class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
  <property name="properties">  
   <props>  
    <prop key="hibernate.hbm2ddl.auto">update</prop>  
    <prop key="hibernate.dialect">  
    org.hibernate.dialect.MySQLDialect</prop>  
    <prop key="hibernate.show_sql">true</prop>  
    <prop key="hibernate.cache.provider_class">  
    net.sf.ehcache.hibernate.EhCacheProvider</prop>  
   </props>  
  </property>  
 </bean>  
 <!-- Hibernate SessionFactory -->  
 <bean id="sessionFactory"  
 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  <property name="dataSource">  
   <ref local="dataSource" />  
  </property>  
  <property name="hibernateProperties">  
   <ref bean="hibernateProperties" />  
  </property>  
  <!-- OR mapping files. -->  
  <property name="mappingResources">  
   <list>  
    <value>myMapping.hbm.xml</value>  
   </list>  
  </property>  
 </bean>  
 <bean id="MyDAOTarget" class="com.dao.MyDAO">  
  <property name="sessionFactory">  
   <ref bean="sessionFactory" />  
  </property>  
 </bean>  

- Define transactionManager for transaction managment as below,

 <bean id="transactionManager"  
 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  <property name="dataSource">  
   <ref bean="dataSource" />  
  </property>  
 </bean>  

- Define org.springframework.transaction.interceptor.TransactionProxyFactoryBean as below, which will eventually make proxy bean on the class given in target property.

 <bean id="MyDAO" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">   
   <property name="transactionManager">   
     <ref bean="transactionManager"/>  
   </property>   
   <property name="target">   
     <ref bean="MyDAOTarget"/>  
   </property>   
   <property name="transactionAttributes">  
     <props>   
       <prop key="insert*">PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED</prop>  
       <prop key="update*">PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED</prop>   
       <prop key="delete*">PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED</prop>  
       <prop key="find*">PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED, readOnly</prop>   
     </props>   
   </property>  
 </bean>  


- Use getHibernateTemplate().get(Class entityClass,Serializable id) OR getHibernateTemplate().load(Class entityClass,Serializable id) to load particular enoty from Database.
- Use getHibernateTemplate().find(String queryString,Object value) to find entities using HQL/SQL query.
- Use getHibernateTemplate().delete(), getHibernateTemplate().deleteAll() for removing entities from Database.
- Use getHibernateTemplate().update() OR getHibernateTemplate().saveOrUpdate() for updating/inserting entity in the Database.

It seems so easy right? But no, wait! All these works fine when you have <property name="defaultAutoCommit" value="true" /> . When you change this property to false, all updates to database will not take effect. Because in that case we need to begin and commit transaction form the code.

I have tried using getHibernateTemplate() for update operations but it failed. It doesn't work with transactions. So try as per the following method.

 public void updateMyObj(MyObj myObj) {  
   Session session = getSession(true);// true means allow create new session if required.  
   Transaction trans = session.beginTransaction();  
   session.saveOrUpdate(myObj);  
   trans.commit();  
 }  

Same thing works for any update operation. This is really strange, i tried for one whole day using getHibernateTemplate() but it never worked with <property name="defaultAutoCommit" value="false" />.

0 Comments: