Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Clarification on Read and None LockMode in Hibernate?

JavaFundaNov 15 2011 — edited Nov 15 2011
As per Read LockMode in Hibernate docs It is a shared lock. Objects in this lock mode were read from the database in the current transaction, rather than being pulled from a cache.

      SessionFactory sessions = new Configuration().configure().buildSessionFactory();
                   Session session = sessions.openSession();
                  Transaction tx = null;
                  tx = session.beginTransaction();
                  Person p1  = (Person)session. get(Person.class,1);//line 1
                  p1  = (Person)session. get(Person.class,1);//line 2
                  session. lock(p1, LockMode.READ);//line 3
                  p1  = (Person)session. get(Person.class,1);//line4 
                  p1  = (Person)session. get(Person.class,1);//line 5 
Now as per shared lock definition holding share locks to prevent concurrent access by a writer with the code snippet above , i made the thread to hold at line 4(so it has acquired the lock in read mode at this point of time).Now i tried to update the person with id 1 from another thread, it succeeded. Not sure why, because as per definition **holding share locks to prevent concurrent access by a writer**


As Read lock mode says Objects in this lock mode were read from the database in the current transaction, rather than being pulled from a cache . Bt in my code snippet at line 4 and line 5 it did not get the row from database for id 1, it got from session only.Why? Basically looking for scenario where developer read lock does help?


Last question on None Lock Mode. As per documentation No lock required. If an object is requested with this lock mode, a READ lock will be obtained if it is necessary to actually read the state from the database, rather than pull it from a cache. As stated no lock acquired then why would developer acquire this lock, as this would be default. Right?

Posted at http://stackoverflow.com/posts/8131208/edit but could not get it better clarified.

Edited by: JavaFunda on Nov 15, 2011 7:25 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 13 2011
Added on Nov 15 2011
0 comments
719 views