Nice and Naughty Transactions in Hibernate and Spring
Join the DZone community and get the full member experience.
Join For FreeI came across a very interesting fact about Spring transaction management. Spring provides us with two ways of handling transaction
- Using proxies,
- Using DI through the getTemplate() method.
Now what happens when we consider 1) we mean:
Declaring in an XML file the transactionManager and transactionTemplate bean:
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" singleton="true">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" singleton="false">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
</bean>
Then we use proxies and provide:
<bean id="myTableManagerTx" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" singleton="true" lazy-init="true">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="target">
<ref bean="myTableManagerImpl"/>
</property>
<property name="transactionAttributes">
<props>
<prop key=" *">PROPAGATION_NOT_SUPPORTED,readOnly</prop>
</props>
</property>
<property name="preInterceptors">
<list>
<ref bean="callMonitor"/>
</list>
</property>
</bean>
With the use of interceptors we can now use doInHibernate() and have transaction support without any extra coding:
protected Object updateObject(final Context ctx, final Object o) {
return getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
return update(ctx, session, o);
}
});
}
However we can use alternative 2)
try {
TransactionTemplate transactionTemplate = SpringFactory.getTransactionTemplate( TransactionDefinition.ISOLATION_READ_COMMITTED, TransactionDefinition.PROPAGATION_REQUIRED, false );
return ( List )transactionTemplate.execute( new TransactionCallback() {
public Object doInTransaction( TransactionStatus transactionStatus ) {
return getMyTableDAO().find( ctx, myId );
}
} );
} catch( Exception e ) {
log.error( e.getMessage(), e );
}
Here we are programmatically creating a transaction based on Spring’s DI.
What happens if we use both together? That is 1) and 2) in an application we could have errors like:Session cannot be closed!
This is because we are using proxies for transactions and also programmatically controlling transactions. If our TransactionDefinition fields :PROPAGATION_REQUIRED, PROPAGATION_NEVER, PROPAGATION_NOT_SUPPORTED,PROPAGATION_REQUIRES_NEW etc are not adjusted and used correctly we could have problems and errors with sessions being closed prematurely and incorrectly controlled. Also the HibernateTemplate controls the sessions for us. As stated in the Spring documentation:
The central method is execute, supporting Hibernate access code implementing the HibernateCallback interface. It provides Hibernate Session handling such that neither the HibernateCallback implementation nor the calling code needs to explicitly care about retrieving/closing Hibernate Sessions, or handling Session lifecycle exceptions. For typical single step actions, there are various convenience methods (find, load, saveOrUpdate, delete).
This can lead to sessions not being closed properly and resources not been freed and hence lead to memory leaks(Which I have experienced!). Final word of advice is: carefully watch your transactions!
Opinions expressed by DZone contributors are their own.
Comments