Recently I have upgrade all my jar files to the latest version (Spring 4.2.x, JPA and Hibernate)
Now I am facing some issues with the test cases. Here is the stackTrace of the test...
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
It says the connection is closed....and cannot rollback. does the pool connection from hibernate auto close the connection? Does anyone have any idea on what is the causing of this?
Edit: Test code added (the test is big if more information is needed I will edit again)
@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");
}
}
}
For the xml file
<?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....
Please stop using H2 in server mode, use it in embedded mode to support @Transactional rollback.
The above exception is thrown because H2 in server mode not supporting Transaction rollback, it is auto-commit by default. Once your query is executed, it is auto-committed and the session is closed. Hence rollback causes the Exception that is thrown.
To use H2 in embedded mode, add the jar to classpath and replace the datasource bean by <jdbc:embedded-database id="dataSource" type="H2" />
. In your case the value will be id="test.dataSource"
.
Here are some known issues in H2 when used in Server Mode:
My recommendation is use a database that well supports transactions with commit and rollback for testing, e.g. MySQL InnoDB.
Please let me know if you continue to have the Exception even after trying this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With