加载实体集合,通过刷新多次保存每个实体

问题描述

我遇到问题-更新相同实体时出现死锁

我想从实体列表中实时更新单个实体上的状态。

我正在为此使用JpaRepository。

简化流程:

class Entity {
  Set<SubEntity> subEntities;

  class SubEntity {
    String status;
  }
}

---

var entities = EntityRepository.loadAll();
for (var entity : entities) {
  for (var subEntity: entity.getSubEntities()) {
    subEntity.setStatus("Prepared for execution"); // In actuality small amount of logic
    EntityRepository.saveAndFlush(entity)
    subEntity.setStatus("Whatever 2"); // In actuality big piece of logic which will save or save-and-retry. Executes logic in lambda; and saveAndFlush is passed as lambda as well "after execution"
    EntityRepository.saveAndFlush(entity)
  }
}

真实代码与上面的代码非常相似。执行此操作使我陷入僵局

2020-08-11 14:42:40.759 UTC [1718] ERROR:  deadlock detected
2020-08-11 14:42:40.759 UTC [1718] DETAIL:  Process 1718 waits for ShareLock on transaction 74137; blocked by process 1717.
        Process 1717 waits for ShareLock on transaction 74138; blocked by process 1716.
        Process 1716 waits for ExclusiveLock on tuple (6,51) of relation 17539 of database 16385; blocked by process 1718.
        Process 1718: update process_step set modify_date=$1 where day=$2 and step_name=$3
        Process 1717: update process_step set modify_date=$1,status=$2 where day=$3 and step_name=$4
        Process 1716: update process_step set modify_date=$1,status=$2 where day=$3 and step_name=$4
2020-08-11 14:42:40.759 UTC [1718] HINT:  See server log for query details.
2020-08-11 14:42:40.759 UTC [1718] CONTEXT:  while locking tuple (6,51) in relation "process_step"
2020-08-11 14:42:40.759 UTC [1718] STATEMENT:  update fees.process_step set modify_date=$1 where day=$2 and step_name=$3
2020-08-11 14:42:40.763 UTC [1717] ERROR:  deadlock detected
2020-08-11 14:42:40.763 UTC [1717] DETAIL:  Process 1717 waits for ShareLock on transaction 74138; blocked by process 1716.
        Process 1716 waits for ExclusiveLock on tuple (6,51) of relation 17539 of database 16385; blocked by process 1719.
        Process 1719 waits for ShareLock on transaction 74137; blocked by process 1717.
        Process 1717: update process_step set modify_date=$1,status=$2 where day=$3 and step_name=$4
        Process 1719: update process_step set modify_date=$1,status=$2 where day=$3 and step_name=$4
2020-08-11 14:42:40.763 UTC [1717] HINT:  See server log for query details.
2020-08-11 14:42:40.763 UTC [1717] CONTEXT:  while updating tuple (6,53) in relation "process_step"
2020-08-11 14:42:40.763 UTC [1717] STATEMENT:  update process_step set modify_date=$1,status=$2 where day=$3 and step_name=$4
2020-08-11 14:42:41.763 UTC [1716] ERROR:  deadlock detected
2020-08-11 14:42:41.763 UTC [1716] DETAIL:  Process 1716 waits for ShareLock on transaction 74139; blocked by process 1719.
        Process 1719 waits for ShareLock on transaction 74138; blocked by process 1716.
        Process 1716: update process_step set modify_date=$1,status=$2 where day=$3 and step_name=$4
2020-08-11 14:42:41.763 UTC [1716] HINT:  See server log for query details.
2020-08-11 14:42:41.763 UTC [1716] CONTEXT:  while updating tuple (6,51) in relation "process_step"
2020-08-11 14:42:41.763 UTC [1716] STATEMENT:  update process_step set modify_date=$1,status=$2 where day=$3 and step_name=$4

为什么呢? saveAndFlush是否不应该在冲突发生之前解决它?

还是问题出在不同的交易上?

编辑:而且,它并不总是发生。好像是比赛条件

编辑2:我已批量更新,但我认为saveAndFlush应该有效地忽略它?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...