尝试通过 JPA 同时保留多个对象时,我遇到了 pesimistlock异常。这是我的代码供参考
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public Boolean changeDplListMappingByCustomWatchList(List<Integer> dplIds, Integer customWatchListId,
ServiceRequestor customServiceRequestor) {
for(Integer dplId : dplIds) {
if(dplId != null) {
CustomWatchListDplMapping customWatchListDplMapping = new CustomWatchListDplMapping();
customWatchListDplMapping.setDplId(dplId);
customWatchListDplMapping.setWatchListId(customWatchListId);
this.create(customWatchListDplMapping);
}
}
}
catch(Exception e) {
LOG.error("Exception occured while changing dpl mapping by custom watchList id", e);
}
return true;
}
public void create(Model entity) {
manager.persist(entity);
manager.joinTransaction();
}
在第一个实体之后,当它遍历第二个实体时,它会引发异常。如果它只有一个实体要保存,那么它运行良好,但对于多个实体模型,它会引发此异常。
默认情况下悲
观锁定为 1 秒,因此请在属性文件中进行更改,它将帮助您解锁,您将能够保存到数据库中