使用SpringJUnit4ClassRunner/MySQL/Spring/Hibernate时,为什么事务没有回滚?

5
我正在进行单元测试,希望所有提交到MySQL数据库的数据都能被回滚...但事实并非如此。即使我的日志显示回滚正在发生,数据仍然被提交了。我已经与这个问题搏斗了几天,所以我的设置已经有了很大的变化,以下是我的当前设置。
LoginDAOTest.java:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:web/WEB-INF/applicationContext-test.xml", "file:web/WEB-INF/dispatcher-servlet-test.xml"})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class UserServiceTest {

  private UserService userService;

  @Test
  public void should_return_true_when_user_is_logged_in ()
          throws Exception
  {
    String[] usernames = {"a","b","c","d"};

    for (String username : usernames)
    {
      userService.logUserIn(username);
      assertThat(userService.isUserLoggedIn(username), is(equalTo(true)));
    }
  }

ApplicationContext-Text.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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
          <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
          <property name="url" value="jdbc:mysql://localhost:3306/******"/>
          <property name="username" value="*****"/>
          <property name="password" value="*****"/>
  </bean>

  <tx:annotation-driven transaction-manager="transactionManager"/>

  <bean id="userService" class="Service.UserService">
    <property name="userDAO" ref="userDAO"/>
  </bean>

  <bean id="userDAO" class="DAO.UserDAO">
    <property name="hibernateTemplate" ref="hibernateTemplate"/>
  </bean>

  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingResources">
      <list>
        <value>/himapping/User.hbm.xml</value>
        <value>/himapping/setup.hbm.xml</value>
        <value>/himapping/UserHistory.hbm.xml</value>
      </list>
    </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"
          p:sessionFactory-ref="sessionFactory"/>

  <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory">
      <ref bean="sessionFactory"/>
    </property>
  </bean>

</beans>

我已经阅读了这个问题,并且已经检查过MySQL数据库表的设置,确保其使用InnoDB。此外,我已经成功地实现了事务回滚,但是在我的测试套件之外,这必须是我设置不正确的原因。
非常感谢您的任何帮助 :)
5个回答

13
问题实际上是连接在事务回滚之前自动提交了。我需要更改我的dataSource bean以包括一个defaultAutoCommit属性:

问题实际上是连接在事务回滚之前自动提交了。我需要更改我的dataSource bean以包括一个defaultAutoCommit属性:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql://localhost:3306/test"/>
  <property name="username" value="root"/>
  <property name="password" value="Ecosim07"/>
  <property name="defaultAutoCommit" value="false" /> 
</bean>

6

对于我来说,defaultAutoCommit和@Transactional都没有起作用。我不得不将数据库类型更改为InnoDB。


3
InnoDB实际上是MySQL中进行事务处理的必需品;默认的数据库类型不支持事务。 - fred-o

1

解决问题的另一种方法:

不要使用:

<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

标签默认创建的是MyISAM表,因此不支持事务。

尝试使用

<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>

创建InnoDB表,从而支持事务。


1

这必须被使用

@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@TestExecutionListeners({ TransactionalTestExecutionListener.class })
@Transactional

TransactionalTestExecutionListener包含isRollback()方法,该方法会在测试方法执行后回滚事务。


0

希望我是对的,这个问题很简单。您的测试类缺少 @Transactional 注释。这意味着测试方法本身不在事务中运行,因此没有任何需要回滚的内容。希望能对您有所帮助。


我已经添加了@Transactional注解,但它仍然在提交事务 :( - Trevor
为了彻底一些...如之前所述,我已经改变了我的设置很多次,我上面发布的设置没有显示事务回滚的日志...当我添加了@Transactional注释后,现在日志中显示事务正在回滚,但仍然提交到数据库。我还不得不微调我的库,我不得不追踪asm.jar文件的3.2版本,因为我得到了一个“方法未找到”的异常ASM库。 - Trevor
特雷弗,你的用户服务本身是否有任何事务注释或控制呢?提交可能是由于事务链中某个地方的REQUIRES_NEW传播值引起的。我想进行另一个测试,就是使用事务测试直接将记录插入到数据库中,确保它正在回滚。如果没有回滚,那么在Hibernate中设置事务时可能会出现问题。你能否提供有关该服务的更多详细信息? - Gennadiy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接