TransactionSystemException:无法回滚Hibernate Transaction;交易未开始

时间:2010-11-30 08:23:02

标签: java hibernate spring transactions java-ee

由于Hibernate,我有一个导致错误的测试。我不知道如何解决这个问题。这是堆栈跟踪:

 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

导致错误的测试如下:

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

这就是applicatipnContext-dao.xml中的事务管理器的样子:

<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>

这是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>

编辑 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"
    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>

(看似相同?)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

1 个答案:

答案 0 :(得分:2)

添加此conf。在你的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应该包含

org.hibernate.transaction true  

测试类应该配置为

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 {  

注意:并非所有字段都可能需要,我刚刚展示了一个工作的配置文件。