打开新的数据库连接还是在过程完成之前保持连接打开状态好吗?

abi*_*964 2 java groovy mybatis

我有2个数据库oldnewold需要对db详细信息进行过滤/处理并存储到中new

OLD DB

  1. 我大约有10000种配置(数据库行)

  2. 和上面匹配的10000个BLOBS(xml文件大小平均为4MB)

NEW DB

  1. 1个新表将包含来自旧过滤器的数据,但是这次没有BLOB数据,而是绝对路径

  2. 并根据配置提出一些建议

在这里,我编写了一个程序(使用Groovy&MyBatis for DB),该程序获取所有可用的配置记录OLD DB并将其存储在类列表中,并且数据库连接已关闭

为了也为每个配置ID提取BLOBS,建立了一个新的连接并长时间保持打开状态

List<String> projectid
List<CSMConfigInfo> oldConfigs
List<ConfigInfo> newConfigs 
Map<String,CSMConfigInfo> oldConfigMap

SqlSession session = DatabaseConnectivity.getOldCSMDBSessionFactory().openSession()
/*  trying to batch execute based on project id */
    projectid.each {pid->
        logger.info "Initiating conversion for all configuration under $pid project id"
        oldConfigMap.each {k,v->
/*  Here I am keeping a DB connection open for a long time */           
            if(pid.equals(v)){                  
                createFromBlob(k,session)                   
            }
        }                       
        logger.info "Completed for $pid project id\n"           
    }

session.close()
Run Code Online (Sandbox Code Playgroud)

在按1提取BLOB 1之后,我创建了一个temp xml文件,该文件经过解析以应用要插入的过滤器NEW DB。在下面的代码中,您可以看到,基于xml是否可转换和可解析,将为打开新连接NEW DB。这是一个好习惯还是我需要NEW DB为所有10000条记录保持连接打开状态?

/* XML is converted to new format and is parsable */
def createFromBlob(CSMConfigInfo cfg,SqlSession oldCSMSession){
    .
    .

    if(xmlConverted&&xmlParsed){
        //DB Entries
        try{
            /* So now here I am opening a new connection for every old config record, which can be 10000 times too, depending on the filter */
            SqlSession sess = DatabaseConnectivity.getNewCSMSessionFactory().openSession()

            //New CSM Config
            makeDatabaseEntriesForConfiguration(newConfig,sess)
            //Fire Rules
            fireRules(newConfig,sess,newCSMRoot)

            sess.close()

        }
        catch(IOException e){
            logger.info "Exception with ${newConfig.getCfgId().toString()} while making DB entries for CONFIG_INFO"
        }
        logger.info "Config id: "+cfg.getCfgId().toString()+" completed successfully, took "+getElapsedTime(startTime)+ " time. $newabspath"
    }
    else{
        def errormsg = null
        if(!xmlConverted&&!xmlParsed)
            errormsg = "Error while CONVERSION & PARSING of config id "+cfg.getCfgId().toString()+", took "+getElapsedTime(startTime)+ " time."
        else if(!xmlConverted)
            errormsg = "Error while CONVERSION of config id "+cfg.getCfgId().toString()+", took "+getElapsedTime(startTime)+ " time."
        else if(!xmlParsed)
            errormsg = "Error while PARSING  of config id "+cfg.getCfgId().toString()+", took "+getElapsedTime(startTime)+ " time."
        logger.info errormsg
    }

    makeDatabaseEntriesForConvertStatus(csmConfigConvertStatus,oldCSMSession)
}
Run Code Online (Sandbox Code Playgroud)

目前,这适用于20条记录,但是我不确定它将对所有10000条记录有何反应。请帮忙

更新资料

每个配置大约需要3-6秒

ram*_*inb 5

使用数据库连接池将总是更加高效,让容器在需要时管理建立和关闭该连接。可以避免使用池来创建连接,执行语句然后关闭该连接,该池会在为您提供要使用的连接之前进行预连接,并且在大多数情况下(尤其是您所描述的情况),无需建立连接它可能已经连接了。

因此,是的,保持连接打开效率更高,甚至可以更好地合并连接...