• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate 2: locking a row

 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need to synchronize object access between 2 VMs.
So I added a special state field that can be "PROCESSING", "NOTPROCESSING".
When it is "NOTPROCESSING" it can be grabbed from the database by one of the VMs and the field of that object will be st to "PROCESSING" so now my code can continue execute because this object will not be grabbed due to the fact that it is in the wrong state. BUT how I ensure that in case of similtaniouse access and before the state change 2 VMs will not grab the same object? If in the same session and transaction I grab the object and change the state help?

Thanks,
John
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to follow what you are attempting here and one thing I wonder is why the synchronization point is in the (seperate) JVMs rather than the (shared) database?
 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Sturrock wrote:I'm trying to follow what you are attempting here and one thing I wonder is why the synchronization point is in the (seperate) JVMs rather than the (shared) database?


ok shared db. I just wanted to make sure people didn't tell me to use synchronized block
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, a synchronized block is always wrong where database access is concerned. If you have to you can use pessimistic locking, but in almost all cases this is not needed (hence my question, rather than just suggesting it).
 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Sturrock wrote:No, a synchronized block is always wrong where database access is concerned. If you have to you can use pessimistic locking, but in almost all cases this is not needed (hence my question, rather than just suggesting it).

so what IS needed then?
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic