dbunit DatabaseSequenceFilter 初始化缓慢

发布于 2024-12-03 20:22:28 字数 221 浏览 0 评论 0原文

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

ITableFilter filter;

....

filter = new DatabaseSequenceFilter(con);

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

什么会造成这个问题呢?

谢谢。

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

ITableFilter filter;

....

filter = new DatabaseSequenceFilter(con);

Other db operations, selections works fine.

What can make this problem?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声静候 2024-12-10 20:22:28

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

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

牛↙奶布丁 2024-12-10 20:22:28

如果您想将行导入 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("================================================================================");
}

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("================================================================================");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文