[hibernate-issues] [Hibernate-JIRA] Created: (HHH-5368) Not throwing Optimistic Lock when the entity is attached

Fernando Rubbo (JIRA) noreply at atlassian.com
Thu Jul 8 14:19:13 EDT 2010


Not throwing Optimistic Lock when the entity is attached
--------------------------------------------------------

                 Key: HHH-5368
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5368
             Project: Hibernate Core
          Issue Type: Bug
          Components: core
    Affects Versions: 3.2.4.sp1
         Environment: windows XP, jboss 4.3 EAP, hibernate 3.4.2.sp1
            Reporter: Fernando Rubbo
            Priority: Minor


Hi

Hibernate is not throwing Optimistic Lock when the entity is attached. It happens when the business rule sets a version in the attached entity that is less than its original (from database) version value.

Take the following code as an example of the bug.
       // inserts A
        A a1 = new A();
        a1.setName("A1");
        dao.persist(a1);
        dao.flush();
        dao.clear();

        // as expected, the piece of code below works just fine
        final A a2 = dao.find(A.class, new BigDecimal(1));
        a2.setName("A2");
        dao.merge(a2);
        dao.flush();
        dao.clear();

        // as expected, this code fails due to Optimistic Lock
        try{
            final A a3 = dao.find(A.class, new BigDecimal(1));
            dao.clear();
            a3.setName("A3");
            a3.setVersion(0); //database verison is 1
            dao.merge(a3);
            dao.flush();
            Assert.fail();
        }catch (Exception e) {
            System.out.println(e.getMessage());
            // ignore
        }finally{
            dao.clear();
        }

        // unexpectedly, the piece of code below also works..
        // even with the version being less than the one in the database
        // Note that this happens because hibernate is holding internaly the original version of the entity. 
        // Even when the version is manually changed it keeps using the original one. 
        final A a3 = dao.find(A.class, new BigDecimal(1));
        a3.setName("A3");
        a3.setVersion(0); //database verison is 1
        dao.merge(a3);
        dao.flush();
        dao.clear();

This problem can be handled by the business rule. For example, whether the version that comes from the screen is smaller than the one from the database it could throw an Optimistic Lock exception. However, it it was not handled, hibernate must take care of this in the same way it does with detached objects.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list