dimanche 2 octobre 2016

Derby Database throwing ERROR 23505 : "duplicate key value in a unique or primary key constraint"

I am using derby for my unit testing purposes, I have configured it to a local file system.

This is my persistence.xml

       <properties>
            <property name="hibernate.hbm2ddl.auto" value="create"/>        
            <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver" />
            <property name="hibernate.connection.url" value="jdbc:derby:../DerbyDatabases/project-Name;create=true" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" />
        </properties>

I read table values form an xml and I clean up the database after each individual test method run in the tearDown method by calling the below

Database population:

  DatabaseOperation.CLEAN_INSERT.execute(IDatabaseConnection, IDataSet);

Database clean up:

 DatabaseOperation.DELETE_ALL.execute(IDatabaseConnection, IDataSet);

in package org.dbunit.operatio.DeleteAllOperation

But even after this, when I run multiple test classes i get random test failures stating

Caused by: org.hibernate.exception.ConstraintViolationException: could not insert: 
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2285)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2672)
    at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
    at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:304)
    ... 20 more
Caused by: org.apache.derby.shared.common.error.DerbySQLIntegrityConstraintViolationException: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL161002132928930' defined on ....
Caused by: ERROR 23505: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL161002132928930'

But these constrains errors dont show up when i run those failing classes individually.

I even tried manual table truncation, like below, between each test, still no progress

for(String tableName : iDataSet.getTableNames())
{
  Statement statement = connection.createStatement();
  statement.execute("truncate table " + tableName);
  statement.close();
}

And also, is there a way to read the log files created by derby, I can at-least take a look at the logs to find what I might be missing here

Aucun commentaire:

Enregistrer un commentaire