Over a million developers have joined DZone.
Platinum Partner

Nice and Naughty Transactions in Hibernate and Spring

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.

I came across a very interesting fact about Spring transaction management. Spring provides us with two ways of handling transaction

  1. Using proxies,
  2. 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" />
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" singleton="false">
<property name="transactionManager">
<ref bean="transactionManager" />

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 name="target">
<ref bean="myTableManagerImpl"/>
<property name="transactionAttributes">
<prop key=" *">PROPAGATION_NOT_SUPPORTED,readOnly</prop>
<property name="preInterceptors">
<ref bean="callMonitor"/>


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!

From http://gabrieljeremiahcampbell.wordpress.com/2010/08/31/nice-and-naughty-transactions-in-hibernate-and-spring/

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}