mysql - 为什么在使用 SpringJUnit4ClassRunner/MySQL/Spring/Hibernate 时事务不回滚

标签 mysql hibernate spring-mvc junit4 rollback

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

任何帮助将不胜感激:)

最佳答案

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

关于mysql - 为什么在使用 SpringJUnit4ClassRunner/MySQL/Spring/Hibernate 时事务不回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2827770/

相关文章:

mysql - 从mysql中删除重复的列值

mysql - 基于来自多个表的数据在 SQL 中更新

hibernate - 无法配置 Jasypt + Hibernate

java - getHibernateTemplate().flush() 函数

java - 如何在 Spring Web MVC 中使用 Ajax JQuery

java - 处理 json 和表单参数的 Spring MVC 方法

MySQL 复制 : Waiting to reconnect after a failed registration on master

php - 使用 AJAX 数据更新数据库

java - Hibernate validator 不起作用

java - 保存时 HttpServletResponse 提示输入文件名