DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Nice and Naughty Transactions in Hibernate and Spring

Nice and Naughty Transactions in Hibernate and Spring

Gabriel Campbell user avatar by
Gabriel Campbell
·
Dec. 03, 10 · Java Zone · Interview
Like (0)
Save
Tweet
13.54K Views

Join the DZone community and get the full member experience.

Join For Free

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

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

Spring Framework Hibernate

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Top 7 Features in Jakarta EE 10 Release
  • Progressive Web Apps vs Native Apps: Differences and Similarities
  • Spring, IoC Containers, and Static Code: Design Principles
  • Top 10 Automated Software Testing Tools

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo