@Transactional in Spring+Hibernate

后端 未结 3 1540
轮回少年
轮回少年 2021-01-02 11:49

I an using Spring 3.1 + Hibernate 4.x in my web application. In my DAO, i am saving User type object as following

sessionFactory.getCurren         


        
相关标签:
3条回答
  • 2021-01-02 12:26

    Using your working version of the Spring XML, and the @Transactional annotated DAO class, are you defining the DAO in your Spring XML ? (Perhaps as a prototype) Because if you're not, then so far as I can see, your DAO is not going to be AOP'ed for transactional aspects. I think that's the easiest way. This example is from the Spring 3 Doc, section 10.5.6 Using @Transactional.

      <!-- this is the service object that we want to make transactional -->
      <bean id="fooService" class="x.y.service.DefaultFooService"/>
    
      <!-- enable the configuration of transactional behavior based on annotations -->
      <tx:annotation-driven transaction-manager="txManager"/>
    

    ...where you might substitute DefaultFooService for your DAO.

    0 讨论(0)
  • 2021-01-02 12:28

    i think you are using Hibernate 4.x then why you are using hibernate 3 transaction manager in application context file?

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

    i think it should be

    <bean id="txManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    

    just try to use hibernate 4 transaction manager along with @Transactional attribute it should work.

    0 讨论(0)
  • 2021-01-02 12:47

    Basically what needs to be done is to remove from the applicationContext.xml file the following line for Hibernate properties:

    <prop key="hibernate.current_session_context_class">thread</prop>
    

    Once that is remove, Hibernate makes use of Spring for transaction management

    Good luck to you all.

    0 讨论(0)
提交回复
热议问题