7

我正在将 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 锁,但是这种情况应该如何解决,当许多参与者应该修改相同的数据库记录时,最后一次修改在哪里获胜?

4

2 回答 2

15

解决方案:

问题:直接从 Play Form 保存分离的 EBean 模型会导致 OptimisticLockException,或者在使用 @Version 时会导致 NullpointerException。

Form<Venue> form = form(Venue.class).bindFromRequest();
form.get().update(id); // this causes the exception

解决方案:表单应该支持在从请求参数绑定之前从数据库中提供一个对象。然后在请求中找到的参数应该覆盖对象上的相关属性。也许在 bindFromRequest() 之前调用 fill():

Form<Venue> form = form(Venue.class).fill(Venue.find.byId(id)).bindFromRequest();
form.get().update(id);
于 2012-10-28T13:50:09.943 回答
2

我解决了这个 OptmistLockException 问题,只是在尝试更新时将实体 ID 传递给控制器​​。为了防止用户更改值,该值作为隐藏字段传递。

 <input type="hidden" name="id" value="@formVar(<identifierVariable>).value"/>

在控制器端,我检查收到的表单是否有错误。在否定的情况下,我更新表单中的附加实体。

 public static Result update() {
     Form<Entity> filledForm = form.bindFromRequest();
     if ( filledForm.hasErrors() ) {
        flashError("app.oh_snap", "app.change_things");
     } else {
        Entity entity = filledForm.get();
        entity.update();
     }
 }
于 2013-04-18T12:48:06.883 回答