【发布时间】:2020-07-18 16:52:05
【问题描述】:
我在乐观锁失败后尝试重试,但 JPA 关闭连接并没有重新连接。
public String updateTnx(Integer id, String txt) {
Tnx tnx = tnxRepository.findById(id);
//record update by other process
tnx.txt = txt;
tnxRepository.save(tnx);
return tnx.toJson();
}
1:在这种情况下,ObjectOptimisticLockingFailureException 被抛出,因为另一个进程已经更新了版本列。
@Retryable(value = {ObjectOptimisticLockingFailureException.class})
public String updateTnx(Integer id, String txt) {
Tnx tnx = tnxRepository.findById(id);
tnx.txt = txt;
tnxRepository.save(tnx);
return tnx.toJson();
}
2:所以我在方法中添加了retryable,但是我得到了StatementIsClosedException。
public String updateTnx(Integer id, String txt) {
try {
Tnx tnx = tnxRepository.findById(id);
tnx.txt = txt;
tnxRepository.save(tnx);
return tnx.toJson();
} catch (ObjectOptimisticLockingFailureException exp) {
Tnx tnx = tnxRepository.findById(id);
tnx.txt = txt;
Ttt ttt = new Ttt();
tttRepository.save(ttt);
tnxRepository.save(tnx);
return tnx.toJson();
}
}
3:我可以将 StatementIsClosedException 添加到可重试,但奇怪的是,如果我在目标表之前放置另一个表进行更新,这也可以。
我的问题是,JPA 是否缓存了导致 StatementIsClosedException 的最后准备好的语句?
【问题讨论】:
标签: java mysql spring hibernate jpa