开发者

slow dbunit DatabaseSequenceFilter initialization

I use Oracle 11 with dbunit. The DatabaseSequenceFilter initialization is about 20 minutes.

ITableFilter filter;

....

filter = new DatabaseSequenceFilter(con);

Other db operations, selections works f开发者_如何转开发ine.

What can make this problem?

Thanks.


The problem was: I used db connection via internet. It was too slow for this.


If you like to import rows into Oracle and you have problems with the order of your data, you could disable all foreign keys and after the import is done, you can enable them again. If something in your data is wrong, you get a error message. I added some Bootstrap helper class. I use Spring and you can initialize this class by setting the dataSource, a flag if the data should be loaded, a properties file with table names of the tables that should be cleaned before import, and the dbunit dataset.

Example call (Spring and the files must be contained in classpath):

<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>

And the class is:

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("================================================================================");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜