DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Frameworks
  4. 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 · Interview
Like (0)
Save
Tweet
Share
13.78K 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

  • How To Create a Failover Client Using the Hazelcast Viridian Serverless
  • Running Databases on Kubernetes
  • Build an Automated Testing Pipeline With GitLab CI/CD and Selenium Grid
  • Seamless Integration of Azure Functions With SQL Server: A Developer's Perspective

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: