Results 1 to 2 of 2

Thread: Problem with Transaction Management

  1. #1
    Join Date
    Jul 2011
    Posts
    1

    Default Problem with Transaction Management

    Hi,

    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>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotati on.AnnotationSessionFactoryBean"
    destroy-method="destroy">
    <property name="dataSource">
    <ref bean="myDataSource" />
    </property>
    <property name="hibernateProperties">
    <props>
    <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>
    </props>
    </property>
    <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    <property name="annotatedClasses">
    <list>
    <value>com.x.y.z.User</value>
    </list>
    </property>
    </bean>

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

    The sessionFactory is autowired to the DAO Impl.

    @Autowired
    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) {
    userDao.saveUser(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.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Use [ code][/code ] tags when posting code so that we can actually read it !!! Also use the search as this question has been answered numerous times before...

    <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.
    No it doesn't it looks for all beans in the applicationcontext and when needed applies an advice (in this case the transactional stuff). Make sure you don't have duplicate instances of the services (do you use component-scanning and do your services have @Service?). Post your configuration (web.xml and both configuration files).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •