使用Spring DBUnit,Spring Data和嵌入式H2数据库时出现回滚错误

时间:2014-03-31 19:31:22

标签: spring spring-data h2 sqlexception spring-test-dbunit

我遇到了古玩错误。

我有一个小的DBUnit测试:

@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));

    }

没有更多。首先,我想搜索一个由DBUnit(wbSchluesselwertData.xml)预先填充的条目。 之后我想在嵌入式H2内存数据库中通过Spring Data CrudRepository保存两个条目。

              

此数据源由HibernateJpaVendorAdapter和适配器本身包含在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>

如果我在上面运行单元测试,则会出现以下错误:

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)

因此,后续尝试持久化实体失败,因为连接已关闭。 是否存在hibernate持久化和使用ob我无视的文件DBUnit之间存在的影响?

感谢任何帮助。 非常感谢提前。

此致 博多

2 个答案:

答案 0 :(得分:0)

在手动配置测试执行侦听器时,需要注册TransactionalTestExecutionListener,否则不会在测试级别上启动事务。当存储库返回原因异常时,这将导致事务已被关闭。

答案 1 :(得分:0)

尝试更改<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="type"> <option >--Select--</option> <option value="1" selected>A</option> <option value="2">B</option> <option value="3">C</option> </select>。这个设置对我有用:

TestExecutionListeners

不再有@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class, TransactionalTestExecutionListener.class }) 错误,我的测试现在在一个事务中执行。在事务结束时(即在每个测试用例之后),DbUnit的org.h2.jdbc.JdbcSQLException: The object is already closed [90007-175]中的更改不会回滚。