慢dbunit DatabaseSequenceFilter初始化

时间:2011-09-12 07:52:25

标签: oracle11g dbunit

我将Oracle 11与dbunit一起使用。 DatabaseSequenceFilter初始化大约需要20分钟。

ITableFilter filter;

....

filter = new DatabaseSequenceFilter(con);

其他数据库操作,选择正常。

什么可以解决这个问题?

感谢。

2 个答案:

答案 0 :(得分:1)

问题是:我通过互联网使用数据库连接。这太慢了。

答案 1 :(得分:0)

如果您想将行导入Oracle并且您的数据顺序有问题,则可以禁用所有外键,导入完成后,您可以再次启用它们。如果数据中的某些内容有误,则会收到错误消息。我添加了一些Bootstrap助手类。我使用Spring,你可以通过设置dataSource来初始化这个类,如果要加载数据则是一个标志,一个属性文件,其中包含应该在导入之前清理的表的表名,以及dbunit数据集。

示例调用(Spring和文件必须包含在类路径中):

<bean id="bootLoader" class="com.esentri.bootstrap.BootDataLoader">
    <constructor-arg index="0"><ref bean="dataSource"/></constructor-arg>         <!-- dataSource -->
    <constructor-arg index="1" value="true"/>                                     <!-- initializeTestData -->
    <constructor-arg index="2" value="${profile.environment}"/>                                     <!-- environmentName -->
    <constructor-arg index="3" value="bootstrap/cleanUpDataSource.properties"/>   <!-- cleanUpDataSourcePath -->
    <constructor-arg index="4" value="bootstrap/defaultData.xml"/>                <!-- dataSourcePath -->
</bean>

课程是:

public BootDataLoader(DataSource dataSource, String initializeTestData, String environmentName, String cleanUpDataSourcePath, String dataSourcePath ){
    logger.info("================================================================================");
    logger.info(" Start Bootstrapping: BootLoader initialized...");


    //skip importing data if this const-arg is set to false or if environment is not development
    if( initializeTestData.equals("false") || ( !environmentName.equals("dev") && !environmentName.equals("test") ) ) {
        logger.warn("--------------------------------------------------------------------------------------------");
        logger.warn("DATABASE DATA IMPORT IS DISABLED (initializeTestData="+initializeTestData.toString()+", environmentName="+environmentName.toString());
        logger.warn("   BootLoading will be terminated...");
        logger.warn("--------------------------------------------------------------------------------------------");
        return;
    }


    logger.warn("--------------------------------------------------------------------------------------------");
    logger.warn("OVERRIDING DATABASE WITH TEST DATA. TURN ON INFO TO SEE DETAILS");
    logger.warn("--------------------------------------------------------------------------------------------");
    logger.info("JVM DEFAULT CHARSET = '" + Charset.defaultCharset() +"'");
    logger.info("cleanUpDataSourcePath = '" + cleanUpDataSourcePath +"'");
    logger.info("dataSourcePath = '" + dataSourcePath +"'");
    logger.info("connect to database...");
    Connection con = null;
    IDatabaseConnection dbUnitCon = null;
    try {
        con = DataSourceUtils.getConnection(dataSource);
        dbUnitCon = new DatabaseConnection( con );

    } catch( DatabaseUnitException  e) {
        e.printStackTrace();
    }
    DatabaseConfig config = dbUnitCon.getConfig();
    config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new Oracle11DataTypeFactory());
    config.setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, false);
    config.setProperty(DatabaseConfig.FEATURE_SKIP_ORACLE_RECYCLEBIN_TABLES, true);



    //LOAD DATASET
    logger.info("load dataSet from dataSource...");
    FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
    builder.setColumnSensing(true);
    IDataSet dataSet = null;
    try {
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        InputStream is = cl.getResourceAsStream(dataSourcePath);
        dataSet = builder.build(is);
    } catch (Exception e) {
        logger.error("Failed loading BootLoad dataSource '"+dataSourcePath+"'");
        e.printStackTrace();
    }
    logger.info("...done");



    //CLEAN TABLES
    logger.info("clean tables defined in cleanUpDataSource...");
    Properties props = new Properties();
    String table;
    String del = "DELETE FROM ";
    try {
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        InputStream is = cl.getResourceAsStream(cleanUpDataSourcePath);
        props.load(is);
    } catch (Exception e) {
        logger.error("Failed loading BootLoad cleanUpDataSource '"+cleanUpDataSourcePath+"'");
        e.printStackTrace();
    }
    Iterator it = props.keySet().iterator();
    Statement stmt;
    while (it.hasNext()){
        table = (String)it.next();
        logger.info("   DELETE FROM "+table);
        try {
            stmt = con.createStatement();
            stmt.execute(del+table);
            stmt.close();
            con.commit();
        } catch (Exception e) {
            logger.error("Error cleaning table '"+table+"'");
            e.printStackTrace();
        }

    }
    logger.info("...done");



    //DISABLE ALL FOREIGN KEYS
    logger.info("disable all foreign keys...");
    try {
        Statement stmtDisableAllConstaints;
        String disableAllConstaints =
                " begin" +
                "   for i in (select constraint_name, table_name from user_constraints where constraint_type ='R' and status = 'ENABLED') LOOP" +
                "     execute immediate 'alter table '||i.table_name||' disable constraint '||i.constraint_name||'';" +
                "   end loop;" +
                " end;";
        stmtDisableAllConstaints = con.createStatement();
        stmtDisableAllConstaints.execute(disableAllConstaints);
        stmtDisableAllConstaints.close();
        con.commit();
    } catch (Exception e) {
        logger.error("Error disabling foreign keys");
        e.printStackTrace();
    }
    logger.info("...done");



    //IMPORT DATA
    logger.info("import data from '"+dataSourcePath+"'");
    try {
        DatabaseOperation.CLEAN_INSERT.execute(dbUnitCon,dataSet);
    } catch (Exception e) {
        logger.error("Error importing data");
        e.printStackTrace();
    }
    logger.info("...done");


    //ENABLE FOREIGN KEYS
    logger.info("enable all foreign keys...");
    try {
        Statement stmtEnableAllConstaints;
        String disableAllConstaints =
                " begin" +
                "   for i in (select constraint_name, table_name from user_constraints where constraint_type ='R' and status = 'DISABLED') LOOP" +
                "     execute immediate 'alter table '||i.table_name||' enable constraint '||i.constraint_name||'';" +
                "   end loop;" +
                " end;";
        stmtEnableAllConstaints = con.createStatement();
        stmtEnableAllConstaints.execute(disableAllConstaints);
        stmtEnableAllConstaints.close();
        con.commit();
    } catch (Exception e) {
        logger.error("Error enabling foreign keys");
        e.printStackTrace();
    }
    logger.info("...done");


    //CLOSE CONNECTION
    logger.info("close connection");
    DataSourceUtils.releaseConnection(con, dataSource);
    logger.info("...done");

    logger.info(" BootLoader ended");
    logger.info("================================================================================");
}