我在JUnit4和MySQL 5.7中使用Spring 2。
在执行JDBCTemplate在数据库中插入记录的测试之后,这个新插入的记录将保留在customers表中。我尝试了不同的变体(例如,将“事务”和“回滚”移动到方法中),但都是一样的。
测试类的代码:
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的代码;
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表是使用以下代码行创建的:
jdbcTemplate.execute("CREATE TABLE customers(" + "id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))");你能告诉我什么能阻止测试完成后的回滚吗?
发布于 2018-04-05 11:15:36
通常,当使用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;发布于 2018-04-05 08:49:24
您可以尝试以下方法:
spring.profile.active=test,DEV..ETC
spring.jpa.hibernate.ddl-auto=create-drop
这将为您生成表,并在完成后,它将删除这些表。
更新
即使您没有使用JPA,也可以在JDPCTemplate中使用JPA配置。
如果要保留现有数据,仍然可以使用
spring.jpa.hibernate.ddl-auto=create
即使使用JPA,应用程序属性中的数据源配置也允许您使用jdpc模板,如下所示
@Autowired
private JdbcTemplate jdbcTemplate;我希望这能帮上忙。
https://stackoverflow.com/questions/49667846
复制相似问题