我正在将 Ebean 与 Play Framework 2 一起使用,有时它会遇到此类 OptimisticLockException:
play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[OptimisticLockException: Data has changed. updated [0] rows sql[update manager set modification_time=?, session_id=?, expiration_date=? where id=? and rating=? and creation_time=? and modification_time=? and name=? and surname=? and login=? and password_hash=? and email=? and session_id=? and expiration_date=?] bind[null]]]
当很少有参与者开始访问数据库时,就会发生这种情况。
因此,Manager 类是:
public class Manager extends Model {
@Getter @Setter
Long id;
@Getter @Setter
private String name;
@Getter @Setter
private String surname;
@Column(unique = true)
@Getter @Setter
private String login;
@Getter @Setter
private String passwordHash;
@Getter @Setter
private String email;
@Embedded
@Getter @Setter
private ManagerSession session;
@Getter
private Timestamp creationTime;
@Getter
private Timestamp modificationTime;
@Override
public void save() {
this.creationTime = new Timestamp(System.currentTimeMillis());
this.modificationTime = new Timestamp(System.currentTimeMillis());
super.save();
}
@Override
public void update() {
this.modificationTime = new Timestamp(System.currentTimeMillis());
super.update();
}
}
使用 save() 和 update() 钩子代替 @PrePersist 注释,因为 Ebean 不支持它。据我所知,@Version 注解总是会带来乐观锁模式,所以我开始使用这种技巧。我知道什么是 Optimistick 锁,但是这种情况应该如何解决,当许多参与者应该修改相同的数据库记录时,最后一次修改在哪里获胜?