Mitul Maheshwari
Mitul Maheshwari

Reputation: 2647

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.

Upvotes: 1

Views: 967

Answers (1)

Karol Dowbecki
Karol Dowbecki

Reputation: 44932

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.

Upvotes: 2

Related Questions