TransactionSystemException: Could not roll back Hibernate Transaction; Transaction not started
21,549
add this conf. in your application-context.xml
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory">
</bean>
hibernate.properties should contain
org.hibernate.transaction true
Test class should be configured like
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/yourapplication-context.xml"})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class MyTest {
Note: not all fields may be required , I have just shown a working conf.
Related videos on Youtube
Author by
Soroush Hakami
Stockholm-based consultant currently building large scale javascript applications, check out syson.se for more info.
Updated on December 01, 2020Comments
-
Soroush Hakami over 3 years
I have a test causing an error because of Hibernate. I have no idea how to resolve this problem. This is the stacktrace:
org.springframework.transaction.TransactionSystemException: Could not roll back Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started at org.springframework.orm.hibernate3.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:677) at org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:823) at org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:800) at org.springframework.test.context.transaction.TransactionalTestExecutionListener$TransactionContext.endTransaction(TransactionalTestExecutionListener.java:501) at org.springframework.test.context.transaction.TransactionalTestExecutionListener.endTransaction(TransactionalTestExecutionListener.java:277) at org.springframework.test.context.transaction.TransactionalTestExecutionListener.afterTestMethod(TransactionalTestExecutionListener.java:170) at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:344) at org.springframework.test.context.junit4.SpringMethodRoadie.runAfters(SpringMethodRoadie.java:307) at org.springframework.test.context.junit4.SpringMethodRoadie$RunBeforesThenTestThenAfters.run(SpringMethodRoadie.java:338) at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:217) at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:197) at org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:143) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:160) at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51) at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44) at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27) at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37) at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) 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: org.hibernate.TransactionException: Transaction not successfully started at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:183) at org.springframework.orm.hibernate3.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:674) ... 24 more
This is what the test causing the error looks like:
@Test @Transactional public void shouldNotBeAbleToAddCandidateWhenEmailAlreadyExistsInDatabase() { Candidate c = new Candidate(); c.setEmail(EMAIL); c.setCompany(CompanyTest.createDefaultCompany()); userDAO.create(c); Candidate candidateFromDb = (Candidate) userDAO.find(c.getId()); assertEquals(c, candidateFromDb); Candidate failingCandidate = new Candidate(); failingCandidate.setEmail(EMAIL); failingCandidate.setCompany(CompanyTest.createDefaultCompany()); boolean foundDuplicate = false; try { userDAO.create(failingCandidate); } catch (DuplicateEmailException duplicateEmailException) { foundDuplicate = true; } assertTrue(foundDuplicate); }
This is what the Transaction Manager in applicatipnContext-dao.xml looks like:
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean>
This is the applicationContext.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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName"> <bean id="dataConfigPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer"> <property name="locations"> <list> <value>classpath:${app.env}.properties</value> </list> </property> <property name="ignoreResourceNotFound" value="true"/> </bean> <bean id="wicketConfiguration" class="com.firmfactory.work.WicketConfiguration"> <property name="configurationMode" value="development" /> <property name="showStacktrace" value="true" /> </bean> <context:component-scan base-package="com.firmfactory.work.daos" /> <import resource="applicationContext-controllers.xml" /> <import resource="applicationContext-services.xml" /> <bean name="dataSource" class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"> <property name="url"> <value>jdbc:mysql://localhost/workt?characterEncoding=UTF-8</value> </property> <property name="user"> <value>work</value> </property> <property name="////"> <!-- <value>work</value> --> <value>////</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="annotatedClasses"> <list> REMOVED SOME MODELS </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- <bean id="liquibase" class="liquibase.spring.SpringLiquibase"> <property name="dataSource" ref="dataSource" /> <property name="changeLog" value="classpath:DbChangeSet.xml" /> </bean>--> <bean id="resumeInformationPriceString" class="java.lang.String"> <constructor-arg value="25"></constructor-arg> </bean> <bean id="candidateInformationPriceString" class="java.lang.String"> <constructor-arg value="75"></constructor-arg> </bean> <bean id="deafaultCompanyId" class="java.lang.Integer"> <constructor-arg value="1"></constructor-arg> </bean> <bean id="uploadPath" class="java.lang.String"> <constructor-arg value="/home/hudson/uploads/"></constructor-arg> </bean> <bean id="logoPath" class="java.lang.String"> <constructor-arg value="../logos/"></constructor-arg> </bean> <tx:annotation-driven /> </beans>
Edited applicationContext.xml as follows:
<?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:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd " default-autowire="byName"> <bean id="dataConfigPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer"> <property name="locations"> <list> <value>classpath:${app.env}.properties</value> </list> </property> <property name="ignoreResourceNotFound" value="true"/> </bean> <bean id="wicketConfiguration" class="com.firmfactory.work.WicketConfiguration"> <property name="configurationMode" value="development" /> <property name="showStacktrace" value="true" /> </bean> <context:component-scan base-package="com.firmfactory.work.daos" /> <import resource="applicationContext-controllers.xml" /> <import resource="applicationContext-services.xml" /> <bean name="dataSource" class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"> <property name="url"> <value>jdbc:mysql://localhost/work?characterEncoding=UTF-8</value> </property> <property name="user"> <value>work</value> </property> <property name="///"> <!-- <value>work</value> --> <value>///</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="annotatedClasses"> <list> <removed propertys of models> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.transaction">true</prop> </props> </property> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- <bean id="liquibase" class="liquibase.spring.SpringLiquibase"> <property name="dataSource" ref="dataSource" /> <property name="changeLog" value="classpath:DbChangeSet.xml" /> </bean>--> <bean id="resumeInformationPriceString" class="java.lang.String"> <constructor-arg value="25"></constructor-arg> </bean> <bean id="candidateInformationPriceString" class="java.lang.String"> <constructor-arg value="75"></constructor-arg> </bean> <bean id="deafaultCompanyId" class="java.lang.Integer"> <constructor-arg value="1"></constructor-arg> </bean> <bean id="uploadPath" class="java.lang.String"> <constructor-arg value="/home/hudson/uploads/"></constructor-arg> </bean> <bean id="logoPath" class="java.lang.String"> <constructor-arg value="../logos/"></constructor-arg> </bean> </beans>
New (seemingly identical?) stacktrace:
org.springframework.transaction.TransactionSystemException: Could not roll back Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started at org.springframework.orm.hibernate3.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:677) at org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:823) at org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:800) at org.springframework.test.context.transaction.TransactionalTestExecutionListener$TransactionContext.endTransaction(TransactionalTestExecutionListener.java:501) at org.springframework.test.context.transaction.TransactionalTestExecutionListener.endTransaction(TransactionalTestExecutionListener.java:277) at org.springframework.test.context.transaction.TransactionalTestExecutionListener.afterTestMethod(TransactionalTestExecutionListener.java:170) at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:344) at org.springframework.test.context.junit4.SpringMethodRoadie.runAfters(SpringMethodRoadie.java:307) at org.springframework.test.context.junit4.SpringMethodRoadie$RunBeforesThenTestThenAfters.run(SpringMethodRoadie.java:338) at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:217) at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:197) at org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:143) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:160) at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51) at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44) at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27) at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37) at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) 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: org.hibernate.TransactionException: Transaction not successfully started at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:183) at org.springframework.orm.hibernate3.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:674) ... 24 more
-
Bozho over 13 yearsyour transaction configuration? code?
-
Soroush Hakami over 13 yearsAdded the code. Where can I find my transaction configuration?
-
Sean Patrick Floyd over 13 yearsProbably in the applicationContext.xml file where the hibernate session factory is defined. Look for something like
<tx:annotation-based />
-
Soroush Hakami over 13 yearsWas it the Transaction Manager that you ment? I added the one from the applicationContext-dao file (the DAO is causing the error). Thanks for your help.
-
-
Soroush Hakami over 13 yearsThanks for your reply. I added this in the applicationContext but got the error: The prefix "p" for attribute "p:sessionFactory-ref" associated with an element type "bean" is not bound.
-
Soroush Hakami over 13 yearsI get "The annotation <annotation> is disallowed for this location" for all of the annotations.
-
jmj over 13 years@Emil where did you get this ?
The annotation <annotation> is disallowed for this location
? -
Soroush Hakami over 13 yearsIn the testclass. I annotated the test and made the proper imports, but still get the message like : "The annotation @RunWith is disallowed for this location"
-
jmj over 13 years@Emil I hope you have imported the same classes, please verify it, Check updated answer
-
Soroush Hakami over 13 yearsYes, the imports look identical. I am currently running JUnit 3 for all my tests, could that be the problem?
-
jmj over 13 years@Emil can be be I am using 4.4 try out with that
-
Soroush Hakami over 13 yearsMy bad. I was annotating the test, not the testclass. The only problem that remains now is this: The prefix "p" for attribute "p:sessionFactory-ref" associated with an element type "bean" is not bound. (in applicationContext.xml)
-
jmj over 13 years@Emil add
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
in your schemalocation section of xml file -
Soroush Hakami over 13 yearsThanks. I added the changes in applicationContext and annotated correctly in the testclass, but the error is still the same. The one part I'm unsure of is the Hibernate property. I tried with: <prop key="org.hibernate.transaction">true</prop> and <prop key="hibernate.transaction">true</prop> but noone of them made any difference. Any clue on what I might be missing?
-
jmj over 13 years@Emil it is not nesessary to add, by same error message you mean the error for xmlns
p:
?? -
Soroush Hakami over 13 yearsSorry, I was unclear. I mean the TransactionSystemException when I run the test.
-
jmj over 13 years@Emil can you post stacktrace for it
-
Soroush Hakami over 13 yearsI added the new stacktrace. Unfortunatly as far as I can tell it's identical to before I made any changes.
-
Soroush Hakami over 13 yearsNo I am not. I've now decided to go with a completely different approach as I don't really see myself finding a solution to this problem any time soon.