Hibernate - How to restrict the concurrent read of the record by multiple users?
We are using Hibernate 3.6.0. We have requirement of retrieve next available record from the table. How do we control retrieving same record by multiple concurrent users. We need to read the next available record and update the status on column (kind of lock) to avoid next user retrieving the same record. We added Version column on the table (Optimistic lock), but it seems controlling on update.
What is the best solution in Hibernate to achieve this?
Thanks in advance. 开发者_Go百科Venkat
Have you tried locking the record in question? Hibernate supports pessimistic locking (SELECT FOR UPDATE
), look at the documentation.
You might also try optimistic locking, it kind of depends on the level of concurrency you expect.
You can add a flag say read (1=true/0=false) and use this flag while retrieving the object like -
where read=0
after select you can update the read flag to restrict the another user to read this record. if you want - you can use version flag like a read flag, since you update the entity after select version will increment and can be used to differentiate the read/unread records.
Let me know if this helps or I missed your problem entirely.
精彩评论