简体   繁体   中英

How to manage the concurrency using hibernate Transaction API

I have the application which is running in the distributed environment. I want to have a scenario like if two simultaneous requests are coming to me to update the table. if the first request successfully updates the table the next request should not get executed/ discard with some message. so the user has to again load updated data and do update operation again. solution I have found one common solution was version control by adding one key in the table as version and compare it every time while updating the table.

I am looking for some other solution as Hibernate is robust framework there will be other ways to achieve this solution.

By reading an article of JBoss there is something called transaction API through which also we can achieve but not getting clarity on how to achieve the solution.

Hibernate can use both optimistic and pessimistic locking as explained in the official documentation, Chapter 5. Locking .

If your database entities are not contended than optimisitc locking with @Version column seems as the right approach. If you require pessimistic locking check the LockMode class. Another option would be to use SELECT FOR UPDATE statement if it's available in your database. However all pessimistic locking approaches are usually more expensive than simple optimistic locking solution due to additional database operations that happen under the hood.

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