我想分析一个情况,我有两个类。一个类是ProcessImpl,它是起点,并在内部调用其他子事务。我不知道发生了什么问题。 processImpl正在导入一些内容并将相关数据写入数据库。春季交易减缓完成过程
规格
Spring的ORM版本:3.2.18.RELEASE。
JDK版本:1.8。
Db:H2(记录任何db相同的性能)。
问题
如果我从ProcessImpl.processStage()
删除@Transactional
过程需要50秒 如果我继续@Transactional
从ProcessImpl.processStage()
过程需要〜15分钟。 不知道为什么会发生这种情况。 我一直试图解决这个问题,因为很长,但没有运气。请看下面的代码。
要求: 完整processStage()
应该完成或完全回滚,即使儿童事务的一个失败。我也收到很多消息:“参与现有交易”。试图通过添加propagation=Propagation.NESTED
到processStage()
来解决这个问题,但没有奏效。
ProcessImpl Class。
public class ProcessImpl {
/*This is the big transaction that calls other transactional stuff from MyServiceImpl
* This is starting point you can say for the process...
*
* If we remove @Transactional from here the process is lightning fast
* With transactional : 15minutes
* Without transactional : 50 seconds
* */
@Transactional
public void processStage(){
MyServiceImpl mp = new MyServiceImpl();
//do some stuff
mp.doWork1();
//do more work
mp.doWork2();
}
}
MyServiceImpl类
class MyServiceImpl{
@Transactional
public void doWork1(){
Object o = doChildWork();
// and more stuff
//calls other class services and dao layers
}
@Transactional
public void doWork2(){
//some stuff
doChildWork2();
doChildWork();
//more work
}
@Transactional
public Object doChildWork(){
return new Object(); //hypothetical, I am returning list and other collection stuff
}
@Transactional
public Object doChildWork2(){
return new Object(); //hypothetical, I am returning list and other collection stuff
}
}
而且,在这里我会得到自我调用的问题,这是不可取的事务?
尝试添加'readonly'为'@ Transactional' – StanislavL
我与其他的选择操作一起做大量插入在这个过程中,因此,将只读了processStage还有效吗? – swapyonubuntu
不,那么readonly不是您的案例的选项 – StanislavL