【问题标题】:Why are transactions not rolling back when using SpringJUnit4ClassRunner/MySQL/Spring/Hibernate为什么使用 SpringJUnit4ClassRunner/MySQL/Spring/Hibernate 时事务不回滚
【发布时间】:2011-02-19 03:27:15
【问题描述】:

我正在进行单元测试,我希望所有提交到 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。此外,我已经能够在我的测试套件之外成功实现事务回滚。所以这一定是我的某种错误设置。

任何帮助将不胜感激:)

【问题讨论】:

    标签: mysql hibernate spring-mvc junit4 rollback


    【解决方案1】:

    问题原来是连接在事务可以回滚之前自动提交。我必须更改我的 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>
    

    【讨论】:

      【解决方案2】:

      对我来说 defaultAutoCommit 和 @Transactional 没有帮助。我不得不将 db 类型更改为 InnoDB

      【讨论】:

      • InnoDB其实是MySQL中事务的一个需求;默认的 db 类型不支持它们。
      【解决方案3】:

      解决问题的另一种方法:

      而不是使用:

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

      ,默认创建 MyISAM 表,因此不支持事务

      尝试使用

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

      ,它创建 InnoDB 表,因此支持事务。

      【讨论】:

        【解决方案4】:

        这个必须用

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

        TransactionalTestExecutionListener 包含 isRollback() 回滚 交易后的测试方法。

        【讨论】:

          【解决方案5】:

          我希望我是对的,这是一个简单的问题。您在测试类上缺少 @Transactional 注释。这意味着测试方法本身没有在事务中运行,因此没有什么可以回滚的。希望这会有所帮助。

          【讨论】:

          • 我添加了@Transactional 注释,它仍在提交事务:(
          • 为了彻底……如前所述,我已经更改了很多次设置,并且我在上面发布的设置没有在日志中显示事务正在回滚...当我添加@Transactional 注释时,它现在显示事务正在日志中回滚,但它仍在提交到数据库。我还必须稍微调整一下我的库,我必须追踪 asm.jar 文件的 3.2 版,因为我得到了 ASM 库的“未找到方法”异常。
          • Trevor,您是否有任何事务注释或用户服务本身的控件。提交可能是由于事务链上某处的 REQUIRES_NEW 传播值。我将运行的另一个测试是使用事务测试将使用用户 DAO 的记录直接插入到数据库中,并确保它正在回滚。如果不是在休眠中设置的事务有问题。您能否提供有关该服务的功能的更多详细信息?
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-07-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多