-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: optimistic locking failed; nested exception is org.hibernate
PostPosted: Mon Dec 10, 2012 5:02 pm 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
I have an integration test that runs fine agains MySql 5, H2 and HSQLDB but fails against Oracle 10g XE.

It fails with the following exception:
Quote:
org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Object of class [com.thalasoft.learnintouch.core.domain.Link] with identifier [225]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)


Here is the test:
Code:
@Before
public void beforeAnyTest() throws Exception {
    linkCategory1 = linkCategoryDao.makePersistent(linkCategory1);
    linkCategory0 = linkCategoryDao.makePersistent(linkCategory0);
}

public void testDelete() {
    assertEquals(2, linkCategoryDao.countAllRows());
    linkCategoryDao.makeTransient(linkCategory0);
    assertEquals(1, linkCategoryDao.countAllRows());
    Link link = new Link();
    link.setName("Google");
    link.setUrl("www.google.com");
    link.setLinkCategory(linkCategory1);
    link = linkDao.makePersistent(link);
    try {
        linkCategoryDao.makeTransient(linkCategory1);
        linkCategoryDao.flush();
        fail();
    } catch (DataAccessException e) {
        logger.debug("Make sure a parent cannot be deleted if currently being used by a child. Catching the DataAccessException thrown by an illigal delete");
    } finally {
        linkCategoryDao.clear();
    }
    assertEquals(1, linkCategoryDao.countAllRows());
    link.setLinkCategory(null);
    link = linkDao.makePersistent(link);
    linkCategoryDao.makeTransient(linkCategory1);
    assertEquals(0, linkCategoryDao.countAllRows());
}


It fails from the last line linkCategoryDao.countAllRows()

It is strange that it works on all database servers except Oracle.

Here is my DAO setup:
Code:
@Override
public Page<LinkCategory> findAll(final int pageNumber, final int pageSize) {
    Criteria criteria = getSession().createCriteria(getPersistentClass());
    criteria.addOrder(Order.asc("name"));
    Page<LinkCategory> page = getPage(pageNumber, pageSize, criteria);
    return page;
}


I thought the cascade all in the mapping was maybe the cause for this error, so I removed it, but it did not change the issue and the error remained.

Here is the mapping:
Code:
<class name="com.thalasoft.learnintouch.core.domain.LinkCategory" table="link_category" dynamic-insert="true" dynamic-update="true">
    <id name="id" type="java.lang.Integer">
        <column name="id" />
        <generator class="native"><param name="sequence">sq_id_link_category</param></generator>
    </id>
    <version name="version" type="int">
        <column name="version" not-null="true" />
    </version>
    <property name="name" type="string">
        <column name="name" length="50" not-null="true" />
    </property>
    <property name="description" type="string">
        <column name="description" not-null="false" />
    </property>
    <set name="links" inverse="true" order-by="list_order" cascade="all">
            <key column="category_id" />
        <one-to-many class="com.thalasoft.learnintouch.core.domain.Link" />
        </set>
</class>


Any clue ?


Top
 Profile  
 
 Post subject: Re: optimistic locking failed; nested exception is org.hibernate
PostPosted: Mon Dec 10, 2012 5:46 pm 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
The mapping for the child object, that is, the Link class, has a cascade="all" attribute.

If I removed it then the test works fine.

The mapping is:
<many-to-one name="linkCategory" not-null="true" class="com.thalasoft.learnintouch.core.domain.LinkCategory" cascade="all">
<column name="category_id" />
</many-to-one>

The mapping is now:
<many-to-one name="linkCategory" not-null="true" class="com.thalasoft.learnintouch.core.domain.LinkCategory">
<column name="category_id" />
</many-to-one>

Because this then made the LinkDaoTest to fail, I added in this LinkDaoTest an explicit persisting of the link category with a:
linkCategory0 = linkCategoryDao.makePersistent(linkCategory0);
and the LinkDaoTest then worked fine.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.