简体   繁体   中英

Hibernate and Concurrency

I have already read the hibernate docs about concurrency and I think, I have understood the available locking methods , but I am not sure how I should implement the following scenario.

Two clients F (fast) and S (slow) access the database and can modify the same objects.

Now, one additional requirement: It is critical to the application that client F performs as fast as possible.

How would you solve the problem?

My problem with optimistic locking: Assume that F tries to update its changes but couldn't do this successfully, because S already updated its data. An exception (StaleObjectStateException) will be thrown from hibernate. I will catch this exception and merge the changes and try exactly the same transaction again, right? Then I don't like the case where F retries its transactions until it was successfull and so F could theoretically block a long time. Should I ignore this and hope that this condition is rare in practise? Or can I give my clients something like a database-locking-priority?

Other users can live with this problems:

StaleObjectStateException (Optimistic Locking) ... we get 3 exceptions per 10000 calls.

Catch the StaleObjectException:s and increase the priority of the thread which should be faster. StaleObjectException:s should be rare. Look into pessimistic locking if this is not working for you.

First thing that jumps out is that if you have a requirement for A+B to perform as fast as possible, you are going to get massive slow down when catching and handling exceptions. That process is very slow.

I would have to read your post many, many more times to fully grasp what you're saying and offer a better solution but for starters, I would immediately consider not working with exceptions in this case.

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