Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with Transaction Management  RSS feed

Selva Duraisamy
Posts: 6
  • Mark post as helpful
  • send pies
  • 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

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) {"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 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(
at org.hibernate.impl.SessionFactoryImpl.getCurrentSe ssion(

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.
Vijitha Kumara
Posts: 3992
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!