简体   繁体   中英

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.

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.

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
  • Optimistic/Pessimistic locking: Use hibernate or SELECT ... FOR UPDATE which I've read is bad for concurrency

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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