求!用spring怎么更好的管理事物。偶遇这道题目, 把我给难住了 ,求各位神人帮忙解答一下!!谢谢了!

解决方案 »

  1.   

    Spring有两种事务管理方式,编程式事务管理和声明式事务管理.编程式事务管理可以实现细粒度的事务控制.声明式事务管理只需要在Spring配置文件中做一些配置,即可将操作纳入到事务管理中,解除了和代码的耦合,这是对应用代码影响最小的选择.这也体现了Spring的AOP
      

  2.   

    希望楼主结贴给分呵呵!
    把这写代码考入到你的applicationContext.xml文件中
    注意把你上面的版本用这段代码替换掉这是spring AOP首先:
    <?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:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">========================================================================
    其次:<!-- 声明式事务帮助业务逻辑实现数据库CUD -->
    <bean id="myTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
         <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="myTxManager">
        <tx:attributes>
          <tx:method name="create*" propagation="REQUIRED"/>
          <tx:method name="update*" propagation="REQUIRED"/>
          <tx:method name="delete*" propagation="REQUIRED"/>
          <tx:method name="read*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
       </tx:advice>
      
       <!-- 将通知给这个切点 advice-ref表示事务通知,pointcut-ref表示谁需要这个事务-->
    <aop:config>
     <!-- 表示过滤aa.cc.*.*(..)包下面的所有方法-->
         <aop:pointcut id="bizServiceMethods" 
         expression="execution(* aa.cc.*.*(..))"/>      <aop:advisor advice-ref="txAdvice" pointcut-ref="bizServiceMethods"/>
       </aop:config>
      

  3.   

    楼主问的范围有些大~我们指的事务 主要是针对数据库操作的
    一般将service层的操作作为一个整体的事务
    当然spring实现事务的原理 离不开AOP的拦截