无法回滚,连接已关闭

时间:2015-09-24 20:33:13

标签: java spring hibernate jpa

最近我将所有jar文件升级到最新版本(Spring 4.2.x,JPA和Hibernate)

现在我面临测试用例的一些问题。这是测试的stackTrace ......

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:548)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:853)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:830)
    at org.springframework.test.context.transaction.TransactionContext.endTransaction(TransactionContext.java:125)
    at org.springframework.test.context.transaction.TransactionalTestExecutionListener.afterTestMethod(TransactionalTestExecutionListener.java:218)
    at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:313)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:93)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:86)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:241)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: javax.persistence.PersistenceException: unexpected error when rollbacking
    at org.hibernate.jpa.internal.TransactionImpl.rollback(TransactionImpl.java:111)
    at org.springframework.orm.jpa.JpaTransactionManager.doRollback(JpaTransactionManager.java:544)
    ... 25 more
Caused by: org.hibernate.TransactionException: rollback failed
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.rollback(AbstractTransactionImpl.java:217)
    at org.hibernate.jpa.internal.TransactionImpl.rollback(TransactionImpl.java:108)
    ... 26 more
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:211)
    ... 27 more
Caused by: java.sql.SQLRecoverableException: Closed Connection
    at oracle.jdbc.driver.PhysicalConnection.rollback(PhysicalConnection.java:3948)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doRollback(JdbcTransaction.java:163)
    ... 28 more

它表示连接已关闭....并且无法回滚。来自hibernate auto的池连接是否关闭了连接?有没有人知道造成这种情况的原因是什么?

编辑: 添加了测试代码(如果需要更多信息,测试很大,我将再次编辑)

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "testConfig.xml" })
@Transactional
@TransactionConfiguration(transactionManager = "test.txManager", defaultRollback = true)
public class ControllerTest {
... logs ... 

    @Autowired
    private Controller controller;
    static final String testIdValue = "114294";
    ... other decorations...

    private Request createRequest() throws Exception {
        Request r = new Request();
        r.setTask(true);
        other set for test values...
        r.assignTaskList(...);
        ...
        return r;
    }

    @Test
    public void assignEndpointsForsSynchTest() throws Exception {

        Request req = createRequest();

        try {
            Response resp = Controller
                    .assignTask(req);
            assertTrue(req.SUCCESSFUL);
        } catch (Exception e) {
            log.info(e.getMessage());
            fail("Unexpected Exception");
        }
    }

}

对于xml文件

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                    ">

    <bean id="log4jInitialization"
        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
        <property name="targetMethod" value="initLogging" />
        <property name="arguments">
            <list>
                <value>classpath:log4j.properties</value>
            </list>
        </property>
    </bean>

    <bean id="test.dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        lazy-init="false">
        <property name="driverClassName" value="org.h2.Driver" />
        <property name="url"
            value="jdbc:h2:mem:test_mem;DB_CLOSE_DELAY=-1;MODE=ORACLE" />
    </bean>

    <jdbc:initialize-database data-source="test.dataSource">
        <jdbc:script location="com/sky/ac/core/engine/comp/schema.sql" />
        <jdbc:script location="com/sky/ac/core/engine/comp/test_data.sql" />
    </jdbc:initialize-database>


    <bean id="test.txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="test.entityMgrFactory" />
        <property name="jpaDialect"> <!-- to support mixed jpaDAO and jdbcTemplateDAO access -->
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
        </property>
    </bean>

    <bean id="test.entityMgrFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="paPersistence" />
        <property name="packagesToScan">
            <list>
                <value>com.sky.ac.core.lock.data</value>
                <value>com.sky.ac.core.vs.dao.jpa</value>
                <value>com.sky.ac.core.sr.data</value>
                <value>com.sky.ac.core.rule.data</value>
                <value>com.sky.ac.core.util.data</value>
                <value>com.sky.ac.core.trace.data</value> 
            </list>
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="ORACLE" />
            </bean>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
                <prop key="hibernate.id.new_generator_mappings">true</prop>
                <prop key="hibernate.connection.release_mode">after_statement</prop>
            </props>
        </property>
        <property name="dataSource" ref="test.dataSource" />
    </bean>

    <tx:annotation-driven transaction-manager="test.txManager"/>

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

....bean class defs....

3 个答案:

答案 0 :(得分:1)

  

请在服务器模式下停止使用H2,在嵌入模式下使用它   支持@Transactional回滚。

抛出上述异常是因为服务器模式下的H2不支持事务回滚,默认情况下是自动提交。执行查询后,将自动提交并关闭会话。因此,回滚会导致抛出异常。

要在嵌入模式下使用H2,请将jar添加到classpath并将数据源bean替换为<jdbc:embedded-database id="dataSource" type="H2" />。在您的情况下,值为id="test.dataSource"

以下是在服务器模式下使用H2时的一些已知问题:

我的建议是使用一个数据库,该数据库很好地支持使用提交和回滚进行测试的事务,例如: MySQL InnoDB

如果您在尝试此操作后继续获得例外,请告诉我。

答案 1 :(得分:0)

Spring 3.2.2 RELEASE(Jira SPR-10395)与Hibernate 4.2有一个非常相似的错误。

但是,您使用另一个Spring版本。问题似乎在 Hibernate实体管理器。(至少在4.2.5最终版本中)。

尝试更改此Hibernate依赖(升级或drowngrade):

 <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-entitymanager</artifactId>
     <version>4.x.x</version>
 </dependency>

答案 2 :(得分:0)

  

无法回滚JPA交易;嵌套异常是javax.persistence.PersistenceException:回滚时出现意外错误。

这意味着有一些断线。

要解决断开连接问题并使jpa能够重新连接到数据库服务器, application.properties 文件应包含以下属性。

spring.datasource.connection-test-query=SELECT 1
spring.datasource.test-while-idle=true
spring.datasource.test-on-borrow=true
spring.datasource.validation-interval=10000
spring.datasource.log-validation-errors=true
spring.datasource.validation-query=SELECT 1