3 Replies Latest reply on Apr 17, 2006 7:25 PM by pomelo verde

    "Optimistic locking"  or "how to catch org.hibernate.StaleOb

    Warg Krumpf Newbie

      Is there a way to know when the EntityManager is internally "discarding" a merge, so we could show the user that his last action failed and he should reload???

      Right now there's just an exception trace getting logged, but we can't catch anything while doing "entityManager.merge(...)"

        • 1. Re: how to catch org.hibernate.StaleObjectException
          Warg Krumpf Newbie

          Sorry, subject was truncated, so i'm posting the question again, with some clarifications:

          Is there a way to know when the EntityManager is internally "discarding" a merge (due to a version conflict), so we could show the user that his last action failed and he should reload???

          Right now there's just an exception trace getting logged, but catched internally (not in our code) so we can't catch anything while doing "entityManager.merge(...).

          • 2. Re:
            Emmanuel Bernard Master

            If the exception is really catched internally, open a bug in JIRA please.

            • 3. Re: If you cannot catch a Hibernate exception
              pomelo verde Newbie

              If you are trying to catch a Hibernate exception and you have failed, remember to flush() the Session inside your method:

              myMethod() {
              try {
              ...
              Session.saveorUpdate(...);
              Session.flush();
              } catch (StaleObjectException ex) {
              ...
              }
              }

              If you don't make a explicit call to flush(), then Hibernate will flush the Session automatically, but after your method call ends, so you will cannot trap the exceptions. Normally this is the case when you use Hibernate from the EntityManager.

              Best regards