[英]Is it good to open a new database connection or keep a connection open until process completes?
我有2个数据库old
& new
, old
分贝细节需要过滤/操纵和存储到new
。
OLD DB
我大约有10000种配置(数据库行)
和上面匹配的10000个BLOBS(xml文件大小平均为4MB)
NEW DB
1个新表将包含来自旧过滤器的数据,但是这次没有BLOB数据,而是绝对路径
并根据配置提出一些建议
在这里,我编写了一个程序(使用Groovy&MyBatis for DB),该程序获取OLD DB
可用的所有配置记录并将其存储在类列表中,并且关闭了DB Connection。
为了也为每个配置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()
在按1提取BLOB 1之后,我创建了一个temp xml文件,该文件经过解析以应用要插入到NEW DB
的过滤器。 在下面的代码中,您可以看到,基于xml是否可转换和可解析,将为NEW DB
打开一个新连接。 这是一种好习惯还是我需要为所有10000条记录保持NEW DB
连接的打开状态?
/* 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)
}
目前,这适用于20条记录,但是我不确定它将对所有10000条记录有何反应。 请帮忙
更新
每个配置大约需要3-6秒
使用数据库连接池将永远更加高效,让容器在需要时管理建立和关闭该连接。 可以避免使用池来创建连接,执行语句然后关闭该连接,该池会在为您提供要使用的连接之前进行预连接,并且在大多数情况下(具体而言,您所描述的情况)无需建立连接它可能已经连接了。
因此,是的,保持连接打开效率更高,甚至可以更好地合并连接...
根据我的经验,在循环的每个周期内创建新连接时通常会产生开销。 如果打开连接需要0.1秒,那么对于10000条记录,您的时间开销将是1000秒(约17分钟)。 如果您在打开10,000条记录时保持打开状态但没有关闭连接,则可以节省17分钟。 我认为您将希望节省17分钟以及关闭和重新创建连接10,000次所需的CPU资源。 在循环外打开连接,然后在循环后关闭连接。
尝试修改方法createFromBlob
,使其将接受两个这样的会话;
def createFromBlob(CSMConfigInfo cfg,SqlSession oldCSMSession, SqlSession newCSMSession){
然后替换此代码块;
/* 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()
有了这个;
//New CSM Config
makeDatabaseEntriesForConfiguration(newConfig,newCSMSession)
//Fire Rules
fireRules(newConfig,newCSMSession,newCSMRoot)
然后,您必须在开始循环之前立即创建传递新的SqlSession,并将其传递给您修改过的方法( createFromBlob
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.