java DbUnit - 警告:AbstractTableMetaData
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3942965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DbUnit - Warning: AbstractTableMetaData
提问by Tim
I am using DbUnit in the latest version 2.4.8 and I get many warnings in my Unit tests with this message:
我在最新版本 2.4.8 中使用 DbUnit,并且在我的单元测试中收到许多警告,并显示以下消息:
WARN : org.dbunit.dataset.AbstractTableMetaData -
Potential problem found: The configured data type factory
'class org.dbunit.dataset.datatype.DefaultDataTypeFactory'
might cause problems with the current database 'MySQL' (e.g. some datatypes may
not be supported properly). In rare cases you might see this message because the
list of supported database products is incomplete (list=[derby]). If so please
request a java-class update via the forums.If you are using your own
IDataTypeFactory extending DefaultDataTypeFactory, ensure that you override
getValidDbProducts() to specify the supported database products.
So I thought I add this (I use a MySQL database):
所以我想我添加了这个(我使用的是 MySQL 数据库):
protected void setUpDatabaseConfig(DatabaseConfig config) {
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
}
But this does not help to avoid these warnings. What's wrong here?
但这无助于避免这些警告。这里有什么问题?
Thank you in advance & Best Regards Tim.
提前致谢 & 最好的问候蒂姆。
回答by reassembler
I solved this with info from the dbunit faq. Just setting the data type factory property made the warning go away.
我用 dbunit faq 中的信息解决了这个问题。只需设置数据类型工厂属性即可使警告消失。
Connection dbConn = template.getDataSource().getConnection();
IDatabaseConnection connection = new DatabaseConnection(dbConn, "UTEST", false);
DatabaseConfig dbConfig = connection.getConfig();
// added this line to get rid of the warning
dbConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new OracleDataTypeFactory());
回答by panser
With Spring-Bootyou can use such configuration bean
使用Spring-Boot,您可以使用这样的配置 bean
@Configuration
public class DbUnitConfiguration {
@Autowired
private DataSource dataSource;
@Bean
public DatabaseDataSourceConnectionFactoryBean dbUnitDatabaseConnection() {
DatabaseConfigBean bean = new DatabaseConfigBean();
bean.setDatatypeFactory(new MySqlDataTypeFactory());
DatabaseDataSourceConnectionFactoryBean dbConnectionFactory = new DatabaseDataSourceConnectionFactoryBean(dataSource);
dbConnectionFactory.setDatabaseConfig(bean);
return dbConnectionFactory;
}
}
回答by Laird Nelson
I know this is an old thread but all the answers here are more complicated than they need to be.
我知道这是一个旧线程,但这里的所有答案都比它们需要的要复杂。
The simplest way to accomplish setting the factory on every connection acquisition is to supplyan OperationListener
and implement its connectionRetrieved
method to do what you want. No overriding needed; the listener will be invoked every time an IDatabaseConnection
is acquired.
在每个连接获取上完成设置工厂的最简单方法是提供一个OperationListener
并实现它的connectionRetrieved
方法来做你想做的事情。无需覆盖;每次IDatabaseConnection
获取an 时都会调用侦听器。
回答by Tridib
I was using JTDS driver and MS SQL 2008. In my DBUntiTest class override the following method. The waring message disappeared.
我正在使用 JTDS 驱动程序和 MS SQL 2008。在我的 DBUntiTest 类中覆盖以下方法。警告信息消失了。
@Override
protected void setUpDatabaseConfig(DatabaseConfig config) {
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MsSqlDataTypeFactory());
}
回答by Forge_7
@reassembler's answer is spot on. Just to add that I am testing against different database products, so I now set the DataType Factory according to the current connection:
@reassembler 的答案是正确的。补充一点,我正在针对不同的数据库产品进行测试,所以我现在根据当前连接设置数据类型工厂:
private IDatabaseConnection getConnection(Connection jdbcConnection) throws Exception {
String databaseProductName = jdbcConnection.getMetaData().getDatabaseProductName();
DatabaseConnection databaseConnection = new DatabaseConnection(jdbcConnection);
DatabaseConfig dbConfig = databaseConnection.getConfig();
switch (databaseProductName) {
case "HSQL Database Engine":
dbConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());
break;
case "MySQL":
dbConfig.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
break;
default:
log.warn("No matching database product found when setting DBUnit DATATYPE_FACTORY");
}
return databaseConnection;
}
You can obviously add any additionaly databases to this list.
显然,您可以将任何其他数据库添加到此列表中。