NonUniqueObjectException during DAO integration test?

Posted by HDave on Stack Overflow See other posts from Stack Overflow or by HDave
Published on 2010-06-14T05:08:46Z Indexed on 2010/06/14 5:52 UTC
Read the original article Hit count: 295

Filed under:
|
|
|
|

I have a JPA/Hibernate application and am trying to get it to run against H2 and MySQL. Currently I am using Atomikos for transactions and C3P0 for connection pooling.

Despite my best efforts my DAO integration tests are failing with org.hibernate.NonUniqueObjectException. I do tend to re-use the same object (same ID even) over and over for all the different tests and I am sure that is the cause, but I can see in the logs that Spring Test and Atomikos are clearly rolling back the transaction associated with each test method.

I would have thought the rollback would have also cleared the persistence context too. On a hunch, I added an a call to dao.clear() at the beginning of the faulty test methods and the problem went away!! Rollback doesn't clear the persistence context...hmmm....

Not sure if this is relevant, but I see a possible autocommit setting problem in the log file:

[20100613 23:06:34] DEBUG [main] SessionFactoryImpl.(242) | instantiating session factory with properties: .....edited for brevity.... hibernate.connection.autocommit=true, ....more stuff follows

Because I am using connection pooling, I figure that Hibernate is where I'll have to indicate I want autocommit off. I found the autocommit property documented here and I put it in my EntityManagerFactory config as follows:

<bean id="myappTestLocalEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="myapp-core" />
        <property name="persistenceUnitPostProcessors">
            <bean class="com.myapp.core.persist.util.JtaPersistenceUnitPostProcessor">
                <property name="jtaDataSource" ref="myappPersistTestJdbcDataSource" />
            </bean>
        </property>

        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="database" value="$DS{hibernate.database}" />
                <property name="databasePlatform" value="$DS{hibernate.dialect}" />
            </bean>
        </property>

        <property name="jpaProperties">
            <props>
                <prop key="hibernate.transaction.factory_class">com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory</prop>
                <prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
                <prop key="hibernate.connection.autocommit">false</prop>
                <prop key="hibernate.format_sql">true"</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
    </property>
</bean>

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about jpa