gpt4 book ai didi

java - Ebean/Play 的 OptimisticLockException

转载 作者:搜寻专家 更新时间:2023-10-30 21:39:57 26 4
gpt4 key购买 nike

我有一个使用 Ebean 的 Play 2.1.3 Java 应用程序。我在下面收到 OptimisticLockException。

[OptimisticLockException: Data has changed. updated [0] rows sql[update person 
set name=? where id=? and email=? and name=? and password is null and created=?
and deleted is null] bind[null]]

我知道它试图告诉我记录在我读取和尝试写入之间发生了变化。但唯一的变化发生在这种方法中。

public void updateFromForm(Map<String, String[]> form) throws Exception {
this.name = form.get("name")[0];

String password = form.get("password")[0];
if (password != null && password.length() != 0) {
String hash = Password.getSaltedHash(password);
this.password = hash;
}

this.update();
}

我做错了吗?我在 zentasks 中看到了类似的逻辑。另外,我是否应该能够看到绑定(bind)变量的值?

更新:我从 Controller 内部调用 updateFromForm():

@RequiresAuthentication(clientName = "FormClient")
public static Result updateProfile() throws Exception {

final CommonProfile profile = getUserProfile();
String email = getEmail(profile);
Person p = Person.find.where().eq("email", email).findList().get(0);

Map<String, String[]> form = request().body().asFormUrlEncoded();

if (p == null) {
Person.createFromForm(form);
} else {
p.updateFromForm(form);
}

return ok("HI");
}

最佳答案

我有另一种方法,我在其中添加注释

@EntityConcurrencyMode(ConcurrencyMode.NONE)

实体类。

这将禁用乐观锁定并发修改检查,这意味着 SQL 变为

update person set name=? where id=?

这更加乐观,因为它只是覆盖了任何中间更改。

关于java - Ebean/Play 的 OptimisticLockException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20339023/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com