编程语言
首页 > 编程语言> > java – 如何禁用JPA的锁定系统?

java – 如何禁用JPA的锁定系统?

作者:互联网

我正在使用OpenJPA,我遇到了锁定问题.我已经理解了什么是OptimisticLockException以及什么时候抛出它.

但我该如何处理呢?

在*下面,您可以找到关于乐观锁定异常的小段落.

简而言之,我如何完全禁用锁管理器?

在我的persistent.xml中,我有以下xml代码,但它不起作用.为什么?

...
<properties>
  <property name="openjpa.LockManager" value="none" />
</properties>
...

*根据关于Java Persistent的wikibook:

Handling optimistic lock exceptions

Unfortunately programmers can frequently be too clever for their own good. The first issue that comes up when using optimistic locking is what to do when an OptimisticLockException occurs. The typical response of the friendly neighborhood super programmer, is to automatically handle the exception. They will just create a new transaction, refresh the object to reset its version, and merge the data back into the object and re-commit it. Presto problem solved, or is it?

This actually defeats the whole point of locking in the first place. If this is what you desire, you may as well use no locking. Unfortunately, the OptimisticLockException should rarely be automatically handled, and you really need to bother the user about the issue. You should report the conflict to the user, and either say “your sorry but an edit conflict occurred and they are going to have to redo their work”, or in the best case, refresh the object and present the user with the current data and the data that they submitted and help them merge the two if appropriate.

Some automated merge tools will compare the two conflicting versions of the data and if none of the individual fields conflict, then the data will just be automatically merged without the user’s aid. This is what most software version control systems do. Unfortunately the user is typically better able to decide when something is a conflict than the program, just because two versions of the .java file did not change the same line of code does not mean there was no conflict, the first user could have deleted a method that the other user added a method to reference, and several other possible issues that cause the typically nightly build to break every so often.

解决方法:

But how can I deal with it ?

这取决于你的应用……你需要做最有意义的事情.也许您需要提示您的用户数据被同时修改,然后重新提交新的(呃)数据?

虽然我不认为禁用OptimisticLocking是正确的解决方案,但我认为设置这两个属性将摆脱您所看到的OLE.

<properties>
  <property name="openjpa.Optimistic" value="false"/>
  <property name="openjpa.LockManager" value="none"/>
</properties>

标签:java,jpa,locking,openjpa
来源: https://codeday.me/bug/20190626/1294504.html