如何禁用JPA的锁定系统? [英] How to disable the lock system of JPA?

查看:103
本文介绍了如何禁用JPA的锁定系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenJPA,但遇到了锁定问题.我已经知道什么是OptimisticLockException以及何时抛出.

I'm using OpenJPA and I have a lock problem. I already understand what's an OptimisticLockException and when it's thrown.

但是我该如何处理呢?

在下面*,您可以找到有关乐观锁异常的一小段内容.

Below*, you can find a small paragraph about the optimistic lock exceptions.

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

In a nutshell, how I can totally disable the lock manager ?

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

In my persistent.xml, I have the following xml code but it does not work. Why ?

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


*根据有关Java持久性的Wikibooks:


*According to the wikibooks about the Java Persistent :

处理乐观锁异常

Handling optimistic lock exceptions

不幸的是,程序员经常为了自己的利益而变得太聪明了.使用乐观锁定时出现的第一个问题是发生OptimisticLockException时该怎么办.友好的邻域超级程序员的典型响应是自动处理异常.他们将只创建一个新事务,刷新对象以重置其版本,然后将数据合并回该对象并重新提交. Presto问题解决了吗?

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?

实际上,这首先使整个锁定点无效.如果您要这样做,最好也不要使用锁定功能.不幸的是,OptimisticLockException很少应该被自动处理,并且您确实需要打扰用户.您应该向用户报告冲突,然后说抱歉,但发生编辑冲突,他们将不得不重做工作",或者在最佳情况下,刷新对象并向用户显示当前数据,并他们提交的数据,并在适当时帮助他们合并两者.

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.

某些自动合并工具将比较数据的两个冲突版本,如果各个字段都没有冲突,则将在没有用户帮助的情况下自动合并数据.这就是大多数软件版本控制系统所执行的操作.不幸的是,用户通常比程序更能确定何时有冲突,仅因为.java文件的两个版本未更改同一行代码并不意味着没有冲突,因此第一个用户可以删除一个另一个用户添加了一个要引用的方法的方法,以及其他可能导致通常每晚构建中断的其他可能问题.

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 ?

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

It depends on your application... you'll need to do what makes the most sense. Perhaps you need to prompt your user that the data was concurrently modified and have then resubmit with the new(er) data?

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

While I don't think disabling OptimisticLocking is the correct solution, I think setting these two properties will get rid of the OLEs you are seeing.

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

这篇关于如何禁用JPA的锁定系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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