I've been stuck in a curios error.
I have a small DBUnit Test:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/testApplicationContext.xml")
@Category(Integrationtest.class)
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionDbUnitTestExecutionListener.class
})
@DatabaseSetup("classpath:db/wbSchluesselwertData.xml")
public class KontaktrolleRepositoryIntegrationTest {
@Autowired
KontaktrolleRepository kontaktrolleRepository;
@Autowired
KontaktBeteiligterRepository kontaktBeteiligterRepository;
@Autowired
WbSchluesselwertRepository wbSchluesselwertRepository;
........
........
@Test
@Transactional
public void that_Kontaktrolle_Is_Inserted_Into_DB() throws Exception {
WbSchluesselwert wbRolle1 = wbSchluesselwertRepository.findOne(new Long(11));
KontaktBeteiligter b1 = new KontaktBeteiligterBuilderAws(erzeugtAm).withName("Posteingang")
.withStandort("Cologne").withOrganization("private").build();
KontaktBeteiligter result = kontaktBeteiligterRepository.save(b1);
Kontaktrolle r1 = new KontaktrolleBuilder(erzeugtAm).withBeteiligter(result).withRollenartId(wbRolle1).build();
Kontaktrolle resultRolle = kontaktrolleRepository.save(r1);
assertNotNull(resultRolle);
assertNotNull(resultRolle.getKontaktrolleId());
assertThat(resultRolle.getKontaktrolleId(), greaterThan(0L));
}
Nothing more. First I want to search an entry which is prefilled by DBUnit (wbSchluesselwertData.xml). And after that I want to save two entries via Spring Data CrudRepository within a embedded H2 memory Database.
This datasource is wrapped by an HibernateJpaVendorAdapter and the adapter itself in a LocalContainerEntityManagerFactoryBean:
<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="packagesToScan" value="de.axa.chronik.persistence.domain" />
</bean>
If I run the unit test above I got the following error:
21:21:55,808 DEBUG JpaTransactionManager:594 - Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@f6b9e9] after transaction
21:21:55,808 DEBUG EntityManagerFactoryUtils:338 - Closing JPA EntityManager
21:21:55,809 WARN TestContextManager:397 - Caught exception while allowing TestExecutionListener [com.github.springtestdbunit.TransactionDbUnitTestExecutionListener@4c4b3e] to process 'after' execution for test: method [public void de.axa.chronik.persistence.repository.KontaktrolleRepositoryIntegrationTest.that_Kontaktrolle_Is_Inserted_Into_DB() throws java.lang.Exception], instance [de.axa.chronik.persistence.repository.KontaktrolleRepositoryIntegrationTest@be41d5], exception [null]
org.springframework.transaction.TransactionSystemException: Could not roll back JPA transaction; nested exception is javax.persistence.PersistenceException: unexpected error when rollbacking
at org.springframework.orm.jpa.JpaTransactionManager.doRollback(JpaTransactionManager.java:544)...............
...........
Caused by: org.hibernate.TransactionException: unable to rollback against JDBC connection
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doRollback(JdbcTransaction.java:167)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.rollback(AbstractTransactionImpl.java:209)
... 33 more
Caused by: org.h2.jdbc.JdbcSQLException: Das Objekt wurde bereits geschlossen
The object is already closed [90007-175]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:332)
So the subsequent attempt to persist the entity fails because the connection has been closed. Are there exsisting effects between hibernate persist and the use ob Spring DBUnit which I disregard?
Any help is appreciated. Many thanks in advance.
Regards, Bodo
Try to change your TestExecutionListeners
. This setup worked for me:
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DbUnitTestExecutionListener.class,
TransactionalTestExecutionListener.class
})
There's no org.h2.jdbc.JdbcSQLException: The object is already closed [90007-175]
error anymore, and my tests are executed inside a transaction now. BUT changes made in DbUnit's @DatabaseSetup
are not rolled back when transaction ends (i.e. after each test case).