title: Rollback error in using Spring DBUnit, Spring Data and embedded H2 database
body: <p>I've been stuck in a curios error.</p>
  
  <p>I have a small DBUnit Test:</p>
  
  <pre><code>@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));
  
      }
  </code></pre>
  
  <p>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.</p>
  
  <p>
      
      
      
  </p>
  
  <p>This datasource is wrapped by an HibernateJpaVendorAdapter and the adapter itself in a LocalContainerEntityManagerFactoryBean:</p>
  
  <pre><code>&lt;bean id="emf"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="jpaVendorAdapter" ref="jpaVendorAdapter" /&gt;
      &lt;property name="packagesToScan" value="de.axa.chronik.persistence.domain" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>If I run the unit test above I got the following error:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>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? </p>
  
  <p>Any help  is appreciated.
  Many thanks in advance.</p>
  
  <p>Regards,
  Bodo</p>