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 6585917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:43:29+00:00 2026-05-25T16:43:29+00:00

I’m trying to exclude two reading threads from read the same record via hibernate.

  • 0

I’m trying to exclude two reading threads from read the same record via hibernate. My SSCCE is as above, but both threads can read the object, while i was expecting that Thread-2 will throw an Exception.

The Store class is my one to create Sessions easily.

I’m testing now with HSQLDB, maybe there is no locking available ?

Update Did what Augusto propose, but still the same. Thread-2 should throw an Exception (?)

        new Thread(new Runnable() { // Thread-1

            @Override
            public void run() {
                Session ses = Store.$.ses();
                Object x = ses.load(Client.class, 1l,
                        new LockOptions(LockMode.PESSIMISTIC_WRITE));
                System.err.println("T1 :"+(x==null));
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException ex) {
                }
                ses.close();
            }
        }).start();

        Thread.yield();

        new Thread(new Runnable() { // Thread-2

            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException ex) {
                }
                Session ses = Store.$.ses();
                Object x = ses.load(Client.class, 1L,
                        new LockOptions(LockMode.PESSIMISTIC_WRITE).setTimeOut(1));
                System.err.println("T2 :"+(x==null));
                ses.close();
            }
        }).start();

Output:

T1: false
t2: false
  • 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-25T16:43:29+00:00Added an answer on May 25, 2026 at 4:43 pm

    LockMode.Read is a shared lock, so all the reads with that lock mode will be able to read from the same source without blocking.

    I think that what you want is an exclusive lock, which uses the LockMode.PESSIMISTIC_WRITE. And HSQLDB supports this type of lock (docs)

    I would also add Thread.yield() between the two threads, to allow the first one to actually start, otherwise the 2nd thread might start before the 1st one you defined.


    I don’t think HSQLDB has a lock timeout, and thus throw an exception when a thread is waiting for a lock for more a given amount of time. What you’ll see is that the ses.load in T2 will always happen after the ses.close from t1.

    If you have a mysql database at hand, you can update the configuration with innodb_lock_wait_timeout = 2 which will cause the DB to throw a lock timeout after 2 seconds.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.