Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6808691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:59:15+00:00 2026-05-26T19:59:15+00:00

As per Read LockMode in Hibernate docs It is a shared lock. Objects in

  • 0

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?

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:59:15+00:00Added an answer on May 26, 2026 at 7:59 pm

    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

    How about when a thread 1 is just about to write an entity to the database and thread 2 is going to read the same entity from it? Either one of them should get a lock to read or write the data. If thread1 gets the write lock, thread 2 must wait till the write is complete. However, if thread 2 gets the read lock, thread 1 has no problem in going in and writing the data. This is so because when thread 2 reads the records in a consistent state. It becomes stale only after thread 1 has written to the database (Although, a write on this object from thread 2 will trigger a stale state exception in case of a versioned entity). Read locks are meant for preventing inconsistency and not freeze the record for further update by the same transaction. You can achieve that behaviour using write locks.

    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?

    Again the read lock is accquired in order to prevent the entity to be in an inconsistent state. From the cache it wont be a problem. The developer does not need to specify the Read lock when doing a fetch, it should be managed internally by the persistence provider.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just read this nice article that taught me how to use inheritance (Table-per-hirarchy).
NOTE: As per ECMAScript5.1, section 15.1.1.3 , window.undefined is read-only. Modern browsers implement this
I've read that OSGI uses separate classloaders per module which allows modules to use
Most solutions I've read here for supporting subdomain-per-user at the DNS level are to
Per man pages, snprintf is returning number of bytes written from glibc version 2.2
Per this helpful article I have confirmed I have a connection pool leak in
Per a great answer from another question I have begun mounting global resources (css/js/images)
Per this question (see comments near the bottom), I was wondering if anyone knows
my C# application reads data from special USB device. The data are read as
I have read a bit about lazy loading in c# and this might seem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.