简体   繁体   English

Mysql 和 java:当涉及到并发更新时如何处理多个用户

[英]Mysql and java : How to handle multiple users when it comes down to concurrent updates

I've looked pretty much everywhere with terms I know but haven't found any good solution.我已经用我知道的术语到处寻找,但没有找到任何好的解决方案。 I'm trying to do a small app that essentially does bookkeeping.我正在尝试做一个基本上做簿记的小应用程序。 I use java for the frontend and MySQL for the database.我使用 java 作为前端,使用 MySQL 作为数据库。

The app has to support multiple users (3-4), therefore I want to implement some kind of protection if two users want to edit the same row.该应用程序必须支持多个用户 (3-4),因此如果两个用户想要编辑同一行,我想实施某种保护。

The things I've looked into and the reasons I don't know if I should use them:我研究过的东西以及我不知道是否应该使用它们的原因:

  • Hibernate: I've seen multiple threads saying ORM are complicating things down the road Hibernate:我看到多个线程说 ORM 正在使事情复杂化
  • Optimistic/Pessimistic locking: Use hibernate or SELECT ... FOR UPDATE which I've read is bad for concurrency乐观/悲观锁定:使用 hibernate 或 SELECT ... FOR UPDATE 我读过对并发不利

I don't know what else to look into.我不知道还有什么可看的。

Thank you for reading感谢您阅读

You could implement some kind of locking the row in the database from your application, eg add a column "lockedby" with an identifier for the client as the value.您可以从您的应用程序中实现某种锁定数据库中的行,例如添加一个列“lockedby”,其中包含一个客户端标识符作为值。 In your application before you grant access to edit the row you check that flag.在您授予编辑行的访问权限之前,在您的应用程序中检查该标志。 The application should re-acquire the lock, if the program is still in edit mode, so if the client fails, the row does not stay locked forever.应用程序应该重新获取锁定,如果程序仍处于编辑模式,那么如果客户端失败,该行不会永远保持锁定状态。 Just an idea, but that's a possible solution, I think.只是一个想法,但我认为这是一个可能的解决方案。

Another way would be to check if the row has changed before you commit your update to it.另一种方法是在提交更新之前检查该行是否已更改。 Could be easily achieved by checking a hashsum of the contents.可以通过检查内容的哈希和轻松实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM