gpt4 book ai didi

mysql - 交易测试不会在完成后回滚

转载 作者:行者123 更新时间:2023-11-29 05:55:40 24 4
gpt4 key购买 nike

我将 Spring Boot 2 与 JUnit4 和 MySQL 5.7 结合使用。

执行 JDBCTemplate 在数据库中插入一条记录的测试后,这条新插入的记录保留在客户表中。我尝试了不同的变体(例如,将 @Transactional 和 @Rollback 移到一个方法中),但结果都是一样的。

测试类代码:

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 的代码: 包 com.example.demo.test;

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

客户表是使用这行代码创建的:

jdbcTemplate.execute("CREATE TABLE customers(" + "id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))");

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

最佳答案

通常,当使用 MySQL 时,@Rollback(或一般情况下回滚事务)不起作用,这是因为在 MySQL 中使用了错误的表类型。

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

InnoDB 引擎支持事务,而 MyISAM 类型不支持。因此,对基于 MyISAM 的表执行回滚不会执行任何操作。

要解决这个问题,要么将 MySQL 中的默认存储引擎设置为 InnoDB,要么在创建表时指定要使用的存储引擎。

CREATE TABLE customers(
id SERIAL,
first_name VARCHAR(255),
last_name VARCHAR(255))
ENGINE = InnoDB;

关于mysql - 交易测试不会在完成后回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49667846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com