Results 1 to 4 of 4

Thread: Problems with merging

  1. #1

    Default Problems with merging (JpaOptimisticLockingFailureException)

    I am using Roo 1.0.2.

    If I call merge() on an entity twice (for instance pressing save button twice in gui) I get a

    org.springframework.orm.jpa.JpaOptimisticLockingFa ilureException: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction

    The entity has not changed or been updated by another.

    If I try to simulate it in a test:

    Code:
       	Person p = new Person();
        	p.setUniqueField("0000000000");
        	p.merge();
        	p.merge();
    I get

    org.springframework.dao.DataIntegrityViolationExce ption: org.hibernate.exception.ConstraintViolationExcepti on: could not insert: [Person]; nested exception is javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationExcepti on: could not insert: [Person]

    So I try another simulation

    Code:
        1	Person p = new Person();
        2	p.setUniqueField("0");
        3	p.merge();
        4	p = p.findPerson(p.getId());
        5	p.merge();
        6	p.merge();
        7	p.merge();
        9	p.setCityName("ZZZ");
       10	p.merge();
    The I get the JpaOptimisticLockingFailureException but not until line 10.

    Can anybody explain to me what is going on here and how to fix this?

    Regards
    Christian
    Last edited by christian@landbo.net; Jul 29th, 2010 at 01:29 PM. Reason: Better title

  2. #2

    Default

    Problem solved.

    Please ignore the unit tests - the problem was something else.

    It was due to this:

    Oracle converts empty string to null but JPA doesn’t update entity cache correspondingly (http://stackoverflow.com/questions/6...cache-correspo)

    /Chr

  3. #3
    Join Date
    Oct 2010
    Location
    Almere, The Netherlands
    Posts
    32

    Default

    I seem to be running into the same problem when a user edits an entity including an empty string field, saves the edit form and then goes back (browser back function) to the form and tries to save again. It only happens when using Oracle 11g and not with MySQL.

    How did you solve it?

  4. #4
    Join Date
    Oct 2010
    Location
    Almere, The Netherlands
    Posts
    32

    Default

    Quote Originally Posted by Harald Walker View Post
    I seem to be running into the same problem when a user edits an entity including an empty string field, saves the edit form and then goes back (browser back function) to the form and tries to save again. It only happens when using Oracle 11g and not with MySQL.
    In my case it was a combination of two issues. Because of how Oracle handles empty strings, the version number of the entity got increased each time (even if there was no change) and if a user uses the back button of the browser, the previous version number was still in the browser cache and this caused the exception. Disabling browser cache for update pages solves that, but then the version number still gets increased, which can be prevented by settings empty string fields to null before merging.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •