简要描述:
- 很多实体未配置此属性导致多线程更新时将不需要更新的字段覆盖回旧值
场景分析:
public class OrderRecon implements Serializable {
@Id
@GeneratedValue
private Long id;
private String txnId;
private String gateId;
}
数据库中记录
temp.jpg
方法一:
OrderRecon order = dao.get(1267l);
order.setTxnId("111");
dao.update(order);
方法二:
OrderRecon order = dao.get(1267l);
order.setGateId("111");
dao.update(order);
此时,多线程情况下,不管先执行哪个方法,线程二在线程一提交之前读出记录再去更新,就会出现将其中一个值更新回去的情况,除非同时加锁,但是这里两个业务更新不同的业务字段其实不存在锁的需求.
解决方案:
添加注解
@DynamicUpdate(true)
public class OrderRecon implements Serializable
注意事项:此方式只有在对象是持久化的情况下才能生效,否则无效,
非持久化的需求情况下请使用
update(final I id, final Map<String, Object> props);
网友评论