如何解决DbUnit的Maven插件在执行过程中出现的侵犯外键约束错误(MySQLIntegrityConstraintViolationException)
来源:互联网 发布:淘宝怎么开直播视频 编辑:程序博客网 时间:2024/04/28 01:53
如果是在使用UbUnit进行单元测试时遇到
其实这个问题不止在通过DbUnit的Maven插件操作数据库时会遇到,在基于DbUni进行单元测试时也会遇到,一般的处理方法是在每次获取连接时前执行一个"set @@session.foreign_key_checks = 0"的statement,以保证本次会话不作外键约束检查,以下是我常用的一个基于DbUnit的单元测试基类,其中第59行就是关于禁止外键约束检查的设置。(注:本例使用的数据库是MySql)
package oobbs.domainmodel;import java.sql.SQLException;import javax.sql.DataSource;import org.dbunit.DataSourceDatabaseTester;import org.dbunit.DefaultOperationListener;import org.dbunit.database.DatabaseConfig;import org.dbunit.database.IDatabaseConnection;import org.dbunit.dataset.xml.XmlDataSet;import org.dbunit.ext.mysql.MySqlDataTypeFactory;import org.junit.After;import org.junit.Before;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.core.io.ClassPathResource;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.transaction.annotation.Transactional;import testutil.ApplicationContextSupport;/** * This base class does not extends any DBTestCase of dbunit,or use any Tester,Their's implement is not good and flexible. * Here,we prepare and set connection manully! * * @author Laurence Geng */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {"classpath:/applicationContext-infrastructure.xml", "classpath:/applicationContext-domainModel.xml", "classpath:/applicationContext-test.xml"})public abstract class DbBasedTest{ /** The data source. */ @Autowired protected DataSource dataSource; /** The dbunitTestUtil can fill test data from xml into test db before testing. */ protected DataSourceDatabaseTester dbunitTestUtil; /** * Inits dbunitTestUtil. * The connectionRetrieved method is called back when setUp() executes. * At this time,we should set connection-specific setting: set foreign key check disabled * so as dbunit can invert test data, and set data type factory be MySqlDataTypeFactory so as * dbunit can convert correct type when invert data to mysql. * * @throws Exception the exception */ protected void initDbunitTestUtil() throws Exception{ dbunitTestUtil = new DataSourceDatabaseTester(dataSource);dbunitTestUtil.setDataSet(new XmlDataSet(new ClassPathResource("dbunit-test-data.xml").getInputStream()));dbunitTestUtil.setOperationListener( new DefaultOperationListener(){ public void connectionRetrieved(IDatabaseConnection connection) { try { //Disable foreign key check!connection.getConnection().prepareStatement("set @@session.foreign_key_checks = 0").execute();// When a new connection has been created then invoke the setUp method// so that user defined DatabaseConfig parameters can be set.connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());} catch (SQLException e) {e.printStackTrace();} }}); }/** * Before test method. * * @throws Exception the exception */@Before public void beforeTestMethod() throws Exception { initDbunitTestUtil();dbunitTestUtil.onSetup(); } /** * After test method. * * @throws Exception the exception */ @After public void afterTestMethod() throws Exception { dbunitTestUtil.onTearDown(); } }
如果是在使用DbUnit的Maven插件时遇到
而如果是使用Maven的DbUnit插件,以命令行的方式执行数据导入工作的话,就只能从数据库连接的url上下手了,方法也很简单就是在原插件的配置上添加foreign_key_checks = 0这个变量,以下是一个例子,请注意第14行<url>标记的部分:<url>${jdbc.url}&sessionVariables=foreign_key_checks=0</url>,它在标准url后面追加了对变量foreign_key_checks的设置。
<plugin><groupId>org.codehaus.mojo</groupId><artifactId>dbunit-maven-plugin</artifactId><version>1.0-beta-3</version><dependencies><dependency><groupId>${jdbc.groupId}</groupId><artifactId>${jdbc.artifactId}</artifactId><version>${jdbc.version}</version></dependency></dependencies><configuration><driver>${jdbc.driverClassName}</driver><url>${jdbc.url}&sessionVariables=foreign_key_checks=0</url><username>${jdbc.username}</username><password>${jdbc.password}</password></configuration><executions><execution><id>default-cli</id><goals><goal>operation</goal></goals><configuration><type>CLEAN_INSERT</type><src>src/test/resources/dbunit-test-data.xml</src><dataTypeFactoryName>org.dbunit.ext.mysql.MySqlDataTypeFactory</dataTypeFactoryName><transaction>true</transaction></configuration></execution></executions></plugin>
备注:
mysql中,变量的作用域有两种session和global,改变变量值的方法为:
要想设置一个GLOBAL变量的值,使用下面的语法:
mysql> SET GLOBAL sort_buffer_size=value;
mysql> SET @@global.sort_buffer_size=value;
要想设置一个SESSION变量的值,使用下面的语法:
mysql> SET SESSION sort_buffer_size=value;
mysql> SET @@session.sort_buffer_size=value;
mysql> SET sort_buffer_size=value;
- 如何解决DbUnit的Maven插件在执行过程中出现的侵犯外键约束错误(MySQLIntegrityConstraintViolationException)
- 终极解决dbunit使用中产生的AmbiguousTableNameException错误
- powerdesign,如何解决PDM生成sql脚本的过程中,无法生成外键约束
- 使用DBUnit测试时违反外键约束的解决办法
- 怎样捕捉存储过程执行过程中出现的错误?
- 解决在phpmyadmin中执行sql语句出现的错误:Unknown storage engine 'InnoDB'
- 解决在phpmyadmin中执行sql语句出现的错误:Unknown storage engine 'InnoDB'
- loadrunner执行场景过程中出现的错误
- scrapy执行过程中出现的错误分析
- requisitePro安装完成后,出现保护侵犯错误的解决方法!
- 在数据库返回过程中,发生如下错误,未能启用约束,一行或多行中包含违反非空、唯一或外键约束的值。
- SQL安装过程中出现的错误解决
- 解决导入过程中出现的ORA-02289错误
- 在eclipse中安装STS插件出现如下错误如何解决?
- 在导入maven项目时出现maven-compiler-plugin:2.3.2:compile插件的错误
- 在导入maven项目时出现maven-compiler-plugin:2.3.2:compile插件的错误
- 解决在idea中使用maven插件时出现乱码
- 新手在Linux安装mysql中如何解决出现mysql: unrecognized service的错误提示
- Git KernelBuild
- 深入探讨 Java 类加载器
- Visual Studio 2008中安装mvc2.0问题排解
- JNA
- 在UBUNTU10.04上安装MATLAB R2011a并使用菜单启动
- 如何解决DbUnit的Maven插件在执行过程中出现的侵犯外键约束错误(MySQLIntegrityConstraintViolationException)
- pb获取软件编译日期(时间)的快捷方法
- c#操作word
- visual C++ 6.0开发工具与调试
- asp.net 动态加载 iframe 改变iframe的src
- 关于js获取父窗体息信的相关方法
- how to deploy webservice onto web application at websphere 6.1
- 两表联合更新 update from
- android手机开发-资料集合