我现在有三个方法A,B,C按顺序执行,现在我将他们放入到同一个事务中,但我期望B有自己单独的事务,也就是说要达到这样的目的:如果C失败了,A、C回滚,B并不回滚。请问如何配事务呢?谢谢

解决方案 »

  1.   

    开始事务1
    调用1
    调用2
    调用3
    提交事务关键在于事务2,你可以
    1 强制使用REQUEST_NEW的配置参数
    2 在B里面自己重新获得事务,代码
      

  2.   

    A,B,C在同一个事务里,要回滚肯定是一起回滚。
    可以在执行A后把执行B所需要的地数据保存下来,
    这样ABC回滚后,将数据给B,让B再执行一次(确保得到的和第一次完全一致的结果),模拟实现B没有回滚的状态。
      

  3.   

    这样子好像一个事务解决不了问题
    给B开启一个单独的事务吧
    A C 可以共用一个事务
      

  4.   

    另外:我在B方法中使用编程式事务,且传播方式设置为TransactionDefinition.PROPAGATION_REQUIRES_NEW好像它吧A、C的那个外面的事务给破坏了
      

  5.   


    同意老大,但是不是传播特性REQUEST_NEW这个是不是应该这么写REQUIRED_NEW ?不知道LZ看懂了么,给 B 指定事务传播为REQUIRED_NEW,不管别人怎么开事务,他自己会再新开一个,这样就不会受别的事务的影响了
      

  6.   

    <?xml version="1.0" encoding="UTF-8"?><!--
    - Application context definition for JPetStore's business layer.
    - Contains bean references to the transaction manager and to the DAOs in
    - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
    -->
    <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/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
        
        <!-- 通过hibernate.cfg.xml配置SessionFactory -->   
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
          <property name="configLocation">
           <value>classpath:hibernate.cfg.xml</value>
          </property>
        </bean>
        
        <!-- 配置事务管理器 -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
           <property name="sessionFactory">
           <!-- 引用 配置SessionFactory的ID-->
             <ref bean="sessionFactory"/>
           </property>
        </bean>
       
        <!-- 配置事务的传播性 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!-- 哪些方法有 事务的传播性-->
          <tx:attributes>
          <!-- 以add开始的方法 REQUIRED表示就用事务,没有就开启事务-->
             <tx:method name="add*" propagation="REQUIRED"/>
             <tx:method name="insert*" propagation="REQUIRED"/>
             <tx:method name="select*" propagation="REQUIRED"/>
             <tx:method name="update*" propagation="REQUIRED"/>
             <!-- 除了上面的方法外,其余的方法都是只读 -->
             <tx:method name="*" read-only="true"/>
          </tx:attributes>
        </tx:advice>
        
        <!-- 哪些类的方法参与了事务 -->
        <aop:config>
        <!-- execution(* com.east.spring.managerimpl.*.*(..))这个类的所有方法都用事务 -->
         <aop:pointcut id="allManagerMethod" expression="execution(* com.east.ssh.dao.impl.*.*(..))"/>
         <!-- 引用 pointcut 和 advice-->
            <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
        </aop:config>
    </beans>
      

  7.   

    事务不能嵌套!你要这样做的话,只能用2个Connection,AC公用一个,B用另外一个
      

  8.   

    我想的也是这样,在b的事务执行时,a,c挂起,b相对于a、c的事务是新的,b的失败不会导致a,c的回滚