【发布时间】:2018-02-23 03:57:56
【问题描述】:
我正在尝试分析我有两个班级的情况。一类是 ProcessImpl,它是起点,在内部调用其他子事务。我不知道出了什么问题。 processImpl 正在导入一些东西并将相关数据写入数据库。
规格
Spring-orm 版本:3.2.18.RELEASE。
JDK 版本:1.8。
Db :H2(在任何数据库上记录相同的性能)。
问题
如果我从 ProcessImpl.processStage() 中删除 @Transactional,则该过程需要 ~ 50 秒
如果我将@Transactional 与ProcessImpl.processStage() 保持一致,则该过程需要~ 15 分钟。
不知道为什么会这样。
很久以来我一直试图解决这个问题,但没有运气。请看下面的代码。
要求:
完整的processStage() 应该完成或完全回滚,即使其中一个子事务失败。
仅供参考:我也收到很多消息,例如:“参与现有交易”。尝试通过将propagation=Propagation.NESTED 添加到processStage() 来解决此问题,但没有成功。
ProcessImpl 类。
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
}
}
另外,这里会出现自我调用问题,这在 Transactional 中是不可取的吗?
【问题讨论】:
-
尝试为
@Transactional添加readonly -
我在这个过程中做了很多插入以及其他选择操作,所以,processStage 的 readOnly 仍然有效吗?
-
不,那么 readonly 不适合您的情况
-
您是否尝试在 processImpl 中仅使用 1 个
@Transactional并删除MyServiceImpl方法中的@Transactional?
标签: java spring transactions transactional spring-orm