0
public class BusinessService {  //spring bean

  public dumpAllData(List){

    /* Complicated DB operation here
     * We dont want to be in transaction now (because of performance issues)
     */ 

    for(...){           //iterating through whole list
      **updateItem(item);**
    }

  }

  public updateItem(Entity e){
    //saves entity into DB
    //we want to be in transaction now
  }

}

弹簧配置:

<tx:advice id="txAdvice" transaction-manager="wsTransactionManager">
    <tx:attributes>           
      <tx:method name="dumpAllData" propagation="NOT_SUPPORTED" />
      <tx:method name="updateItem" propagation="REQUIRES_NEW" />
    </tx:attributes>
</tx:advice>

是否可能有嵌套的 REQUIRED_NEW 传播,该传播将从具有传播 NOT_SUPPORTED 的方法中调用?

问题是我们在 dumpAllData() 中运行了一个广泛的数据库操作(~ 100Mb),所以我们不想在事务中(否则这将是性能问题)。但是我们希望在 updateItem 方法(我们只做简单的实体更新)中的事务(回滚/提交)。

4

2 回答 2

0

我看不出是否在交易中对性能有影响。您是否测量了性能差异,或者您只是在猜测?

无论如何,如果你真的需要这样做,那么updateItem方法应该在另一个 Spring bean 中,注入到BusinessServicebean 中。

实际上,Spring 只有在通过代理调用 bean 方法时才能启动/提交事务。如果你从同一个 bean 的另一个方法调用一个 bean 方法,Spring 不能拦截调用并做它的事务管理。

于 2012-01-09T14:27:43.450 回答
0

如果从同一类的某个方法调用,则更新方法中的事务注释将不会被 Spring 事务基础结构拦截。有关 Spring 事务如何工作的更多理解,请参阅Spring Transaction

于 2017-08-08T12:38:56.093 回答