我在spring3.0整合hibernate3.3  老是报异常 说是save is not valid without active transacton其中在UserService类里面 加入事物的方法如下: @Transactional(propagation=Propagation.REQUIRED)
public void save()
{

this.userDAO.saveUser();
this.logDAO.saveLog();
}在测试的时候 拿到的又是一个代理类  但是没有在方法上加事物 报异常说save is not valid without active transacton
这个问题有的人遇到了 有的人确能正常运行 难道和spring合hibernate的版本有关吗

解决方案 »

  1.   

    你的spring配置文件里,没有配置transactionManager
      

  2.   

    试试在配置文件里加入 <bean id="jpaTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <property name="dataSource" ref="dataSource" />
    </bean>
      

  3.   

    应该是spring配置文件里,没有配置transactionManager吧``
      

  4.   

    我配置了transactionManager的   <bean id="txManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory" />
    </bean>
      

  5.   

    .......晕了   还是不行  完整配置如下 
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    ">
    <!-- <bean name="us" class="com.bean.UserService"></bean> <property
    name="userDAO"> <ref bean="ud"/> </property> </bean>
    --> <bean id="user" class="com.bean.UserDAOAsepect"></bean>
    <context:annotation-config />
    <context:component-scan base-package="com.bean"></context:component-scan>
    <!--
    <aop:config> <aop:pointcut id="userPointcut"
    expression="execution(public void com.bean.UserDAOImpl11.saveUser())"
    /> <aop:aspect id="userAspect" ref="user"> <aop:before method="before"
    pointcut-ref="userPointcut" /> <aop:after method="after"
    pointcut="execution(public * com.bean..*.saveUser(..))" />
    </aop:aspect> </aop:config>
    --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName">
    <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
    <value>jdbc:mysql://127.0.0.1:3306/hibernate</value>
    </property>
    <property name="username">
    <value>root</value>
    </property>
    <property name="password">
    <value>123456</value>
    </property>
    <!--
    <property name="initialSize"> <value>3</value> </property> <property
    name="maxActive"> <value>100</value> </property> <property
    name="maxIdle"> <value>5</value> </property> <property
    name="maxWait"> <value>10</value> </property>
    -->
    </bean> <bean name="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
    <list>
    <value>com.bean.User</value>
    <value>com.bean.UserLog</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.hbm2ddl.auto">create</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <prop key="hibernate.current_session_context_class">thread</prop>
    </props>
    </property>
    </bean>

    <bean id="txManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="txManager" />



    <!--
    <aop:config> <aop:pointcut id="businessPointcut"
    expression="execution(public * com.bean..*.save(..))" /> <aop:advisor
    advice-ref="txAdvice" pointcut-ref="businessPointcut" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method
    name="save" /> </tx:attributes> </tx:advice>
    -->

    </beans>
      

  6.   

    去掉Hibernate.cfg.xml文件中的
    <property name="current_session_context_class">thread</property>
      

  7.   

    问题解决了,多谢fengzhisha0914。可是不知道为什么,不是说如果用getCurrentSession(),要配置这个参数,让session与当前线程关联吗?
      

  8.   

    我也有同样的问题,但是我工程里根本就没有Hibernate.cfg.xml文件啊。
      

  9.   


    去掉<property name="current_session_context_class">thread</property>
    类中就不能通过sessionfaction.getCurrentSession()获得session,而sessionfaction.OpenSession()又是打开新的session,当一个事物要管理多个方法时,比如异常回滚的问题,怎么解决???
    我是菜鸟学习中同样出了这个错误???save is not valid without active transacton
      

  10.   

    去掉spring配置文件中的:<prop key="hibernate.current_session_context_class">thread</prop>
      

  11.   

    在声明式事务管理中,配置了 <tx:annotation-driven transaction-manager="txManager" />  current_session_context_class 默认的是thread,想想也应该是这样,因为spring帮我们做事务管理
    是通过同一个connection 的,只有从当前线程中查找。所以不用配置这项。