问题
I'm using OpenJPA and I have a lock problem. I already understand what's an OptimisticLockException and when it's thrown.
But how can I deal with it ?
Below*, you can find a small paragraph about the optimistic lock exceptions.
In a nutshell, how I can totally disable the lock manager ?
In my persistent.xml, I have the following xml code but it does not work. Why ?
...
<properties>
<property name="openjpa.LockManager" value="none" />
</properties>
...
*According to the wikibooks about the Java Persistent :
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.
回答1:
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?
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>
来源:https://stackoverflow.com/questions/7201940/how-to-disable-the-lock-system-of-jpa