解决方案 »

  1.   

    spring AOP自动完成事务的提交和异常回滚
      

  2.   

    那么我写的baseDao,getCurrentSession的异常是怎么回事,我的事务已经交给spring管理了
      

  3.   

    那么我写的baseDao,getCurrentSession的异常是怎么回事,我的事务已经交给spring管理了既然用spring管理事务,为啥不用spring提供的hibernateTemplate或者jdbcTemplate实现数据的操作呢?
      

  4.   

    1:propagation="REQUIRED", 定义了事务的传播途径(调用其它业务时是否开启新的事务等等)。
    2:spring AOP自动完成事务的提交和异常回滚
    3:你的配置可以啊<prop key="hibernate.current_session_context_class">
                        org.springframework.orm.hibernate3.SpringSessionContext
                    </prop> 就是告诉hibernate 事务交由spring管理, 也就是通过aop实现hibernate Session的创建和关闭。
      

  5.   

    第3个问题,我和你配置的一样,但sessionFactory.getCurrentSession()的时候,抛出了org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here异常,理论上来说,事务已经由spring管理了,为何还会抛出这个异常
      

  6.   

    那么我写的baseDao,getCurrentSession的异常是怎么回事,我的事务已经交给spring管理了既然用spring管理事务,为啥不用spring提供的hibernateTemplate或者jdbcTemplate实现数据的操作呢?我想用hibernate API来操作session,这样有更大的灵活性,现在的问题是,我调用getCurrentSession会抛出异常,而我不知道如何解决,希望朋友指点迷津
      

  7.   

    第3个问题,我和你配置的一样,但sessionFactory.getCurrentSession()的时候,抛出了org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here异常,理论上来说,事务已经由spring管理了,为何还会抛出这个异常
    那你aop配置是否对呢?  是否已经切在对应方法上? 
      

  8.   

    第3个问题,我和你配置的一样,但sessionFactory.getCurrentSession()的时候,抛出了org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here异常,理论上来说,事务已经由spring管理了,为何还会抛出这个异常
    那你aop配置是否对呢?  是否已经切在对应方法上? 
    <!-- 配置事务的传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="save*" propagation="REQUIRED" read-only="false" />
    <tx:method name="delete*" propagation="REQUIRED" read-only="false" />
    <tx:method name="update*" propagation="REQUIRED" read-only="false" />
    <tx:method name="*" read-only="true" />
    </tx:attributes>
    </tx:advice>

    <!-- 把切面注入到事务中 -->
    <aop:config> 
       <aop:pointcut id="productServiceMethods" expression="execution(* com.ssh.*.*(..))" /> 
       <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" /> 
    </aop:config> 
    然后就是帖子中的baseDao的getCurrentSession抛出异常了,我调用的是save方法,和aop的配置也是相符的
      

  9.   

    expression="execution(* com.ssh.*.*(..))"   指定的切点是否正确?  就是说是否切到了你的业务方法上?  只有执行了这里aop切入的方法,spring才会开起事务,也就是会创建Hibernate Session, 保证你后面调用getCurrentSession能够获取到Session。你可以断点到TransactionInterceptor的invoke方法里面,验证你的方法是否被aop切入事务。
    还有如果被aop Interceptorlan拦截到的话, 是可以在方法的行号旁边看aop拦截标识的。 一个类似于"("的符号。Multiple ers at this line
    - implements com.xxxx.xxx.xxxService.xxxx
    - advised by 
     org.springframework.transaction.interceptor.TransactionInterceptor.invoke(org.aopalliance.intercept.MethodInvocation)鼠标放到符号上还有上面的提示哦
      

  10.   


    拦截器没走,而且好像tomcat启动的时候,也没有实例化拦截器,我缺少TransactionInterceptor的配置吗?