• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Problem with Transaction Management

Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am trying to create a simple application which registers a user using Spring 3, Hibernate and mySQL. I am trying to do as per the instrauctions given in http://static.springsource.org/sprin...ansaction.html

using the annotations. The service implementation class is marked with @Transactional

and the Transaction Manager is defined using XML configuration like:

<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager"/>

<bean id="txManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotati on.AnnotationSessionFactoryBean"
<property name="dataSource">
<ref bean="myDataSource" />
<property name="hibernateProperties">
<prop key="hibernate.dialect">org.hibernate.dialect.HSQL Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.jdbc.use_streams_for_binary">true</prop>
<prop key="hibernate.jdbc.batch_size">1500</prop>
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
<property name="annotatedClasses">

the Datasource is pointing to MySQL db.

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3308/xyz"/>
<property name="username" value="root"/>
<property name="password" value="root"/>

The sessionFactory is autowired to the DAO Impl.

private SessionFactory sessionFactory;

and when I try to get the current session like:

public void saveUser(User user) {
logger.info("Registering the user : "+sessionFactory.getCurrentSession());
sessionFactory.getCurrentSession().saveOrUpdate(us er);

it throws the exception:

[#|2011-07-26T13:09:28.579+0100|SEVERE|glassfish3.0.1|javax.e nterprise.system.std.com.sun.enterprise.v3.service s.impl|_ThreadID=30;_ThreadName=Thread-1;|org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionCo ntext.currentSession(SpringSessionContext.java:63)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSe ssion(SessionFactoryImpl.java:700)

so it seems that the transaction is not created correctly. This is the point where the session needs to be created, becasue its kind of entry point to the applciation.

The Service class is annonated with:

@Transactional(readOnly = true)
public class UserServiceImpl implements UserService{

and the method is annonated with:

@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false)
public void addUser(User user) {

is there anything else I have missed out? The documentation has a Note:

<tx:annotation-driven/> only looks for @Transactional on beans in the same application context it is defined in. This means that, if you put <tx:annotation-driven/> in a WebApplicationContext for a DispatcherServlet, it only checks for @Transactional beans in your controllers, and not your services.

but all my xml configurations are part of db.xml which is imported in the root-context.xml.

I have tried defining <tx:advice> as well. But the error persisted. any help regarding this would be greatly appreciated.

thanks in advance.
Posts: 4116
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the complete link please? And please make sure to use code tags when posting code in the forum.
    Bookmark Topic Watch Topic
  • New Topic