首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >事务测试完成后不会回滚

问事务测试完成后不会回滚
EN

Stack Overflow用户
提问于 2018-04-05 08:40:27
回答 2查看 171关注 0票数 1

我在JUnit4和MySQL 5.7中使用Spring 2。

在执行JDBCTemplate在数据库中插入记录的测试之后,这个新插入的记录将保留在customers表中。我尝试了不同的变体(例如,将“事务”和“回滚”移动到方法中),但都是一样的。

测试类的代码:

代码语言:javascript
复制
import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;


@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {TestConfig.class})
@Transactional
@Rollback
public class TestJDBCTemplate
    {

    @Autowired
    PlatformTransactionManager transactionManager;

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    ApplicationContext applicationContext;


    @Test
    public void testInsert()
        {
        jdbcTemplate.execute("INSERT INTO customers (first_name, last_name) VALUES (\"Jeff\", \"Johnson\")");

        // This condition is irrelevant
        assertEquals(1, 1);
        }

    }

ContextCOnfiguration: package com.example.demo.test的代码;

代码语言:javascript
复制
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration
public class TestConfig
    {

    @Bean
    DataSource dataSource()
        {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/thdb");
        dataSource.setUsername("root");
        dataSource.setPassword("root");

        return dataSource;
        }

    @Bean
    PlatformTransactionManager transactionManager()
        {
        return new DataSourceTransactionManager(dataSource());
        }

    @Bean
    JdbcTemplate jdbcTemplate(DataSource dataSource)
        {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        return jdbcTemplate;
        }
    }

Customers表是使用以下代码行创建的:

代码语言:javascript
复制
jdbcTemplate.execute("CREATE TABLE customers(" + "id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))");

你能告诉我什么能阻止测试完成后的回滚吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-05 11:15:36

通常,当使用MySQL时,以及@Rollback (或通常回滚事务)不能工作的事实,这是因为在MySQL中使用了错误的表类型。

新版本默认使用InnoDB存储引擎,但是旧版本(或对JPA提供程序使用错误的方言)将使用MyISAM存储引擎。

InnoDB引擎支持事务,而MyISAM类型不支持事务。因此,在基于MyISAM的表上进行回滚不会做任何事情。

要修复MySQL中的默认存储引擎为InnoDB,或者在创建表时指定要使用的存储引擎。

代码语言:javascript
复制
CREATE TABLE customers(
  id SERIAL, 
  first_name VARCHAR(255), 
  last_name VARCHAR(255))
ENGINE = InnoDB;
票数 1
EN

Stack Overflow用户

发布于 2018-04-05 08:49:24

您可以尝试以下方法:

  1. 添加一个新的应用程序配置文件

spring.profile.active=test,DEV..ETC

查看正式的Spring文档

  1. 在测试配置文件中,使用新的db凭据进行测试,并使用以下hibernate属性

spring.jpa.hibernate.ddl-auto=create-drop

这将为您生成表,并在完成后,它将删除这些表。

更新

即使您没有使用JPA,也可以在JDPCTemplate中使用JPA配置。

如果要保留现有数据,仍然可以使用

spring.jpa.hibernate.ddl-auto=create

即使使用JPA,应用程序属性中的数据源配置也允许您使用jdpc模板,如下所示

代码语言:javascript
复制
 @Autowired
private JdbcTemplate jdbcTemplate;

我希望这能帮上忙。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49667846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档