我有2個數據庫old
& new
,old
數據庫細節需要過濾/操縱並存儲到new
。在過程完成之前打開新的數據庫連接或保持連接處於打開狀態很好嗎?
OLD DB
我有大約10000配置(DB行)
和10000 BLOBS(XML文件的大小是4MB上的平均值)匹配配置ID的從上方
NEW DB
- 是將包含來自舊過濾後的數據
1新表,但這次沒有BLOB數據,而不是絕對路徑
和每個配置一些建議
這裏我寫了一個程序(使用Groovy & MyBatis for DB),它可以獲得OLD DB
中的所有配置記錄,並存儲在類的列表和數據庫連接被關閉
爲了得取的斑每個配置ID,一個新的連接建立&保持打開很長一段時間
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()
獲取的BLOB後1乘1,我創建一個臨時的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條記錄。請幫助
更新
我會針對每個配置
感謝您的回覆。 MyBatis不關心連接池?你知道我可以關注數據庫連接池的任何資源嗎? – abi1964
我會查看http://grails.org/plugin/jdbc-pool,以達到您的要求。或者,您可以自己創建多個連接,並且不要關閉它們直到代碼完成所需的任何操作... – ramsinb
MyBatis連接池的任何資源。我沒有使用Grails :( – abi1964