Nice and Naughty Transactions in Hibernate and Spring

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="" 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!



About gjcbell

I am the Cloud Architect at IBM. I work in and create Hybrid Cloud solutions. These are solutions that have an on premise facet and multi Cloud platform facet. My area of focus spans the Bluemix Platform, IBM Cloud Private as well as cloud brokerage.
This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s