• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling Session.lock(object, LockMode.READ)

 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers ,

Hibernate provides a feature Object Locking feature
Means that when you don’t want to perform an update on an object , if the object isn’t modified then use session.lock mechanism .

Please tell me how does this actually work??
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Locking is implemented by Hibernate by executing queries appended by "for Update" or "for Read Only" or other clauses.
These clauses may differ for different databases.
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not get you Rahul.
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this similar to using Transactions like Transaction_Serializable --- etc
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using an Oracle database, and do the query, select * from Table where <condition>, it will not lock any rows.
but if you do the query like, Select * from Table where <condition> for update;
it will lock those records, so that no other client will be able to do an update on that row untill the first client commits the transaction.
So, when you put a certain type of the Lockmode on an object entity and try to fetch the row using that LockMode, Hibernate will make the corresponding query as Select * from Table where <condition> for update; instead of Select * from Table where <condition>.
Similarly, Hibernate will append different clauses to the original query "Select * from Table where <condition>" depending on your Lock mode(Read or Write).
Further, these clauses are different for different Databases, Ex. DB2 uses "Select * from Table where <condition> with RR"....where RR means repeatable Read...you dont need to worry about that...
So, in short, Hibernate implements the Locking mechanism by adding appropriate clauses(like for Update, for Read only) to the actual query. Also, these clauses differ depending on the databases that you use.

Hope it helps.
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to give a better idea..
Go to any Oracle Client...like TOAD....and assuming that the table Message has a record for id = 1, execute a query like "Select * from Message where id = 1 for update";
do not commit(assuming auto commit is off)
Go to the sqlplus (or any other Oracle Client) and execute the same query
"Select * from Message where id = 1 for update";
you will see that the query keeps on waiting for the 1st client to commit..
So, that means, adding "for update" in the query, locked the rows.
Hibernate locks the records in a similar way, appends "for update" or other appropriate clauses to the original query.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic