如何解决DbUnit的Maven插件在执行过程中出现的侵犯外键约束错误(MySQLIntegrityConstraintViolationException)
2011-07-27 14:05
1061 查看
如果是在使用UbUnit进行单元测试时遇到
其实这个问题不止在通过DbUnit的Maven插件操作数据库时会遇到,在基于DbUni进行单元测试时也会遇到,一般的处理方法是在每次获取连接时前执行一个"set @@session.foreign_key_checks = 0"的statement,以保证本次会话不作外键约束检查,以下是我常用的一个基于DbUnit的单元测试基类,其中第59行就是关于禁止外键约束检查的设置。(注:本例使用的数据库是MySql)
如果是在使用DbUnit的Maven插件时遇到
而如果是使用Maven的DbUnit插件,以命令行的方式执行数据导入工作的话,就只能从数据库连接的url上下手了,方法也很简单就是在原插件的配置上添加foreign_key_checks = 0这个变量,以下是一个例子,请注意第14行<url>标记的部分:<url>${jdbc.url}&sessionVariables=foreign_key_checks=0</url>,它在标准url后面追加了对变量foreign_key_checks的设置。
备注:
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插件操作数据库时会遇到,在基于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;
相关文章推荐
- mybatis generator MySQL 自增ID出现重复问题MySQLIntegrityConstraintViolationException
- Mysql创建表或外键约束时出现1005错误代码的解决方法
- 新手在Linux安装mysql中如何解决出现mysql: unrecognized service的错误提示
- MySQLIntegrityConstraintViolationException: Column 'id' in field list is ambiguous
- 解决Maven打包时出现Maven无法下载插件的错误
- MySQLIntegrityConstraintViolationException
- 系统提示java.sql.SQLIntegrityConstraintViolationException: ORA-00001: 违反唯一约束条件
- 执行Hive时出现org.apache.hadoop.util.RunJar.main(RunJar.java:136) Caused by: java.lang.NumberFormatException: For input string: "1s"错误的解决办法(图文详解)
- 如何解决sql server 2000安装过程中出现 安装程序配置服务器失败 错误
- MySQLIntegrityConstraintViolationException: Column 'ID' in where clause is ambiguous
- IDEA的maven项目出现org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)错误解决方法
- 遇到"Unhandled exception in app.exe (QTGUID4.DLL): 0xC0000005: Access Violation"错误的解决过程
- maven中出现web.xml is missing and <failOnMissingWebXml> is set to true的错误如何解决
- MySQLIntegrityConstraintViolationException: Duplicate entry '***' for key 'PRIMARY'
- Ajax方法执行跳转或者加载操作系统报出这样错误Sys.WebForms.PageRequestManagerParserErrorException:如何让解决
- maven运行tomcat6出现错误Exception starting filter encodingFilter怎么解决
- Maven使用tomcat7-maven-plugin插件run时出现错误: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component
- com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry '04140700' for
- 【报错】MySQLIntegrityConstraintViolationException:Column cannot be null
- 智能安全实验室-全能优化(Guardio)错误解决(1):“出现异常:EXCEPTION_ACCESS_VIOLATION=Access Violation”