Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Panel

IMPORTANT: Load vs Get

with two sessions working with an object with the same identifier. Initial version for object in DB is 10.

Code Block

 with two sessions working with an object with the same identifier. Initial version for object in DB is 10.



    Session 1                  Session 2

    ---------                  ---------

    Load object

    Wait a while..

                               Load object

                               Modify object property

                               [triggers db 'select' -

                                version read as 10]

                               Commit

                               [triggers db update,

                                version modified to 11]

    Modify object property

      [triggers db 'select' -

      version read as 11]

    Commit

      [triggers db update,

      version modified to 12]

We actually want session 1's commit to fail with an optimistic lock exception, but it will succeed here.

Using "get" instead of "load" works around the problem, because get will immediately issue a select, and the version numbers will be loaded at the correct times for the optimistic lock checking.

We actually want session 1's commit to fail with an optimistic lock exception, but it will succeed here.

Using "get" instead of "load" works around the problem, because get will immediately issue a select, and the version numbers will be loaded at the correct times for the optimistic lock checking.blah

Panel

Further Reading

indent
1
1
Some useful information is available here:
* [Hibernate (3.3) Documentation|http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/objectstate.html]
* ["Hibernate in Action" PDF|https://web.mit.edu/sturner/www/secure/HibernateInAction.pdf]
* ["Java Persistence with Hibernate" PDF|https://web.mit.edu/sturner/www/secure/JavaPersistenceWithHibernate.pdf]