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方法(我们只对实体进行简单的更新(中进行事务(回滚/提交(。
我看不出是否在事务中对性能有何影响。您是否测量了性能差异,或者只是猜测?
无论如何,如果你真的需要这样做,那么updateItem
方法应该在另一个春豆中,注入BusinessService
豆中。
事实上,Spring 只有在通过代理调用 Bean 方法时才能启动/提交事务。如果你从同一个bean的另一个方法调用一个bean方法,Spring 无法拦截调用并执行其事务管理。
如果从同一类的某个方法调用,则更新方法中的事务注释不会被 Spring 事务基础设施拦截。有关 Spring 事务如何工作的更多了解,请参阅 Spring 事务。