如何处理锁(JPA)?

发布于 2024-12-01 22:23:11 字数 992 浏览 2 评论 0原文

根据Java持久/锁定wikibooks*,最好的处理方法with locks 的作用是向用户报告乐观锁错误/异常。

问题是它不可扩展。假设我有很多用户,他们可能会通过相同的操作导致锁定。用户不关心锁定错误消息。

简而言之:

  • 最好的方法是禁用所有锁?
  • 最好的方法是向用户报告错误锁定消息?但用户必须重试他的操作,直到它起作用!
  • 最好的方法是重试事务,直到没有锁?

*

处理乐观锁异常

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

这实际上从一开始就破坏了锁定的全部意义。如果这是您想要的,您也可以不使用锁定。不幸的是,OptimisticLockException 很少会被自动处理,并且您确实需要就这个问题打扰用户。您应该向用户报告冲突,并要么说“抱歉,但发生了编辑冲突,他们将不得不重做工作”,或者在最好的情况下,刷新对象并向用户提供当前数据和他们提交的数据,并在适当的情况下帮助他们合并两者。

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

According to the Java Persistent/Locking wikibooks*, the best way to deal with locks is to report the Optimistic Lock Error/Exception to the user.

The problem is that it's not scalable. Suppose that I have many users who are likely to cause a lock with the same action. The user does not care about the lock error message.

In a nutshell :

  • The best way is to disable all locks ?
  • The best way is to report to the user the error lock message ? But the user must retry his action until it will work !
  • The best way is to retry the transaction until there's no lock ?

*

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心碎无痕… 2024-12-08 22:23:11

用户会关心这个消息,因为他想做一些修改,而修改还没有进行。因此,他将刷新页面以查看数据的新状态,并将重做他的修改,或者决定在新状态下不再进行修改。

如果两个用户同时修改一个实体,并且无论修改是什么,如果最后一个修改获胜,这会出现问题吗?如果出现问题,请使用乐观锁定,并在出现问题时通知用户。没有办法解决这个问题。

如果这不是问题,那么就不要使用乐观锁。如果最后的修改没有破坏数据库中的约束,那么它总是会获胜。但是,让并发用户修改相同的数据总是会导致异常(例如,因为某些用户可能会在其他用户提交对同一实体的修改之前删除一个实体)。

重试不是一个选择:

  • 要么它会再次失败,因为不可能进行修改
  • ,要么它会成功,但会首先破坏乐观锁定的意义。

您的问题可以用汽车类比来解释。假设您选择购买带有限速器的汽车,以确保您不会超速。现在你问:但我不在乎速度限制。我不应该总是禁用限速器吗?可以,但是如果您被警察抓住,请不要感到惊讶。

The user will care about the message, because he wanted to do some modification, and the modifications have not been made. He will thus refresh the page to see the new state of the data, and will redo his modifications, or decide they should not be made anymore given the new state.

Is it a problem if two users modify an entity concurrently, and if the last modification wins, whatever the modification is? If it is a problem, then use optimistic locking, and inform your user when there is a problem. There's no way around it.

If it's not a problem, then don't use optimistic locking. The last modification, if it doesn't break constraints in your database, will always win. But having concurrent users modifying the same data will always lead to exceptions (for example, because some user might delete an entity before some other user submits a modification to the same entity).

Retrying is not an option:

  • either it will fail again, because it's just not possible to do the modifications
  • or it will succeed, but will defeat the point of having optimistic locking in the first place.

Your problem could be explained with a car analogy. Suppose you choose to buy a car with a speed limiter, in order to make sure you don't break the speed limit. And now you ask: but I don't care about the speed limits. Shouldn't I always disable the speed limiter? You can, but then don't be surprised if you get caught by the police.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文