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

  • Home
  • SEARCH
  • 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 6706569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:32:08+00:00 2026-05-26T07:32:08+00:00

I have 2 concurrent threads that at the same time enter a (Spring) transaction

  • 0

I have 2 concurrent threads that at the same time enter a (Spring) transaction service.

Using Hibernate, the service method loads some entities, processes those, finds one and deletes it from the DB. The pseudo code is as following:

@Transactional
public MyEntity getAndDelete(String prop) {
    List<MyEntity> list = (List<MyEntity>)sessionFactory
        .getCurrentSession()
        .createCriteria(MyEntity.class)
        .add( Restrictions.eq("prop", prop) )
        .list();

    // process the list, and find one entity
    MyEntity entity = findEntity(list);
    if (entity != null) {
        sessionFactory.getCurrentSession().delete(entity);
    }
    return entity;
}

In case two threads at the same time pass the same parameter, both will “find” the same entity an both will call delete. One of them will fail throwing an org.hibernate.StaleObjectStateException when the session is close.

I would like that both threads would return the entity, with no exception being thrown. In order to achieve this, I tried to lock (with “select… for update”) the entity before deleting it, as following:

@Transactional
public MyEntity getAndDelete(String prop) {
    List<MyEntity> list = (List<MyEntity>)sessionFactory
        .getCurrentSession()
        .createCriteria(MyEntity.class)
        .add( Restrictions.eq("prop", prop) )
        .list();

    // process the list, and find one entity
    MyEntity entity = findEntity(list);
    if (entity != null) {
        // reload the entity with "select ...for update"
        // to ensure the exception is not thrown
        MyEntity locked = (MyEntity)sessionFactory
            .getCurrentSession()
            .load(MyEntity.class, entity.getId(), new LockOptions(LockMode.PESSIMISTIC_WRITE));
        if (locked != null) {
            sessionFactory.getCurrentSession().delete(locked);
        }
    }
    return entity;
}

I use load() instead of get() since according to the hibernate APIs, get would return the entity if already in the session, while load should re-read it.

If two threads enter at the same time the method above, one of them blocks a the locking stage, and when the first thread closes the transaction, the second is awoken throwing an org.hibernate.StaleObjectStateException. Why?

Why does the locked load not just return null? How could I achieve this?

  • 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-26T07:32:09+00:00Added an answer on May 26, 2026 at 7:32 am

    I spent some time investigating this issue and I finally understood what happens.

    The PESSIMISTIC_WRITE lock tries to “lock” the Entity that is already loaded in the session, it does not re-read the object from the DB. Debugging the call, I saw that entity == locked returned true (in Java terms). Both variables were pointing to the same instance.

    To force hibernate to reload the Entity, it must be removed from the session first.

    The following code does the trick:

    @Transactional
    public MyEntity getAndDelete(String prop) {
        List<MyEntity> list = (List<MyEntity>)sessionFactory
            .getCurrentSession()
            .createCriteria(MyEntity.class)
            .add( Restrictions.eq("prop", prop) )
            .list();
    
        // process the list, and find one entity
        MyEntity entity = findEntity(list);
        if (entity != null) {
    
            // Remove the entity from the session.
            sessionFactory.getCurrentSession().evict(entity);
    
            // reload the entity with "select ...for update"
            MyEntity locked = (MyEntity)sessionFactory
                .getCurrentSession()
                .get(MyEntity.class, entity.getId(), new LockOptions(LockMode.PESSIMISTIC_WRITE));
            if (locked != null) {
                sessionFactory.getCurrentSession().delete(locked);
            }
        }
        return entity;
    }
    

    The PESSIMISTIC_WRITE mut be used with get instead of load, because otherwise a org.hibernate.ObjectNotFoundExceptionwould be thrown.

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

Sidebar

Related Questions

I have a fixed thread pool that runs 7 concurrent threads at any time
I have multiple threads each one with its own private concurrent queue and all
I have a multithreaded application that has many concurrent operations going on at once.
I have some problems on a site with the concurrent access to a list.
Under JBoss 4.0.1SP1, I have a servlet that makes multiple, concurrent calls to web
If we have a method: public void doSomething(){ synchronized(this){ //some code processing here }
I'm writing an application that will have to be able to handle many concurrent
I have some resource which I'd like to protect from concurrent usage both by
ASP.NET does not allow concurrent requests for the same session; meaning that a user
It's my understanding that if two threads are reading from the same piece of

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.