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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:54:13+00:00 2026-06-15T01:54:13+00:00

I am playing with a standard optimistic concurrency control scenario with extended session /

  • 0

I am playing with a standard optimistic concurrency control scenario with extended session / automatic versioning. I have an entity which I load in the first transaction, present to user for modification and save in the second one, both transactions sharing the same session. After the entity is somehow modified session.flush() at the end of the second transaction may throw a StaleObjectStateException in case a version inconsistency is detected meaning that a concurrent transaction has saved a next version of the entity in between.

I want to handle such an error in a most simple way — just to reload entity losing current changes and continue with editing and saving again. First I tried this:

session.refresh(entity);

but after I modify and attempt to save this refreshed entity, I still get the same StaleObjectStateException, even though it does get refreshed and the version number appears consistent; yes I know that using refresh() in extended sessions is discouraged, but don’t understand why. Is this behavior related to the reason it is discouraged?

Next I tried the following way to avoid using session.refresh():

session.evict(entity);
entity = session.load(MyEntity.class, id);

but it still results in StaleObjectStateException being raised at saving the entity which is not indeed stale.

The only way I managed to cope with the exception is this:

session.clear();
entity = session.load(MyEntity.class, id);

but isn’t session.clear() the same as session.evict() pertaining to my concrete entity?

To resume, my questions are:

  • Why is StaleObjectStateException still thrown on a reloaded entity unless session.clear() is done?
  • What is the correct way to reload an entity which has already been loaded in the same session and why is refresh() bad? Is there something wrong with this approach to implement conversation?

I’m using Hibernate 4.1.7.Final, with no second-level cache.

My apologies if my question is repeating, but I fail to find a profound explanation…

  • 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-06-15T01:54:14+00:00Added an answer on June 15, 2026 at 1:54 am

    When you get an exception in a session, than that session instance is broken. You cannot use that instance any more and you have to throw it away and create a new instance. The exception is not reset (as you can see you get the same exception again thought logically this should not happen). This is a general rule for using hibernate sessions. The reason for this is, hibernate does not always see why an exception appears and the state of the session instance may be inconsistent.

    I don’t know why it works after clear(). This may be accidentally. It is more prudent to use a new instance.

    If you use a StatelessSession, then you don’t have this restriction, but stateless sessions have other disadvantages, for example no caching.

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

Sidebar

Related Questions

I have an FFmpeg-based video-playing app which is able to play content from any
I have been playing with jstree (1.0rc2)+jquery (1.4.2) for the first time with c#.net
I have been playing around with variadic templates in the new c++ standard and
Scenario: I want to have an enum containing all the playing cards in a
While playing with standard library i've found a strange difference between python2 and python3.
I'm a newbie at Flash, so started playing with a pretty standard code sample:
Playing with jquery for the first time, and I'm trying to get a simple
I'm playing around new c++ standard. I write a test to observe behavior of
I've been playing around with Simple.Data and have run across something that I can't
I have been playing around with the min3D libraries a bit lately learning how

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.