Ebean和Play Framework 2的OptimisticLockException [英] OptimisticLockException with Ebean and Play Framework 2

查看:81
本文介绍了Ebean和Play Framework 2的OptimisticLockException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Ebean与Play Framework 2结合使用,有时它会因为此类OptimisticLockException而落入

I am using Ebean with Play Framework 2 and sometimes it falls with OptimisticLockException of such kind:

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]]]

当很少有参与者开始访问数据库时,就会发生这种情况.

This happen when few actors start to access database.

因此,Manager类是:

So, Manager class is:

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

save() and update() hooks used instead @PrePersist annotations, because of Ebean doesn't support it. As I know @Version annotation allways brings Optimistic lock mode, so I start to use such trick. I know what Optimistick lock is, but how this situation should be solved, when many actors should modify same db record, where last modification wins?

推荐答案

问题:直接从播放窗体中保存分离的EBean模型会导致OptimisticLockException,或者在使用@Version时会导致NullpointerException.

The problem: Saving a detached EBean model directly from the Play Form causes either OptimisticLockException, or when using @Version it causes NullpointerException.

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

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

The solution: The form should support supplying it an object from database, before binding from the request parameters. The parameters found in the request should then overwrite the relevant properties on the object. Perhaps call fill() before bindFromRequest():

Form<Venue> form = form(Venue.class).fill(Venue.find.byId(id)).bindFromRequest();
form.get().update(id);

这篇关于Ebean和Play Framework 2的OptimisticLockException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆