我想弄清楚使用PreparedStatement executeBatch()方法的最佳方式。使用PreparedStatement的最佳方法executeBatch()
一種方法我試過是:
try{
prepStmt1 = conn.prepareStatement("update table set done='yes' where phone=?");
while (operatorsQuery.next()) {
logger.info("phone: "+ phone + ", operator: " + operator);
process(); //0.5-1 second long
prepStmt1.setString(1, "0"+phone);
prepStmt1.addBatch();
}
prepStmt1.executeBatch();
}
catch{...}
finally{
closeStatmentand(prepStmt1);
}
我對這段代碼遇到的問題是,該方案可以在中間退出,那麼它可能達不到則ExecuteBatch()方法。
第二種方式我想:
try{
prepStmt1 = conn.prepareStatement("update table set done='yes' where phone=?");
while (operatorsQuery.next()) {
logger.info("phone: "+ phone + ", operator: " + operator);
process(); //0.5-1 second long
prepStmt1.setString(1, "0"+phone);
prepStmt1.addBatch();
if ((j + 1) % 100 == 0) {
prepStmt1.executeBatch();
}
}
prepStmt1.executeBatch();
}
catch{...}
finally{
closeStatmentand(prepStmt1);
}
這是做到這一點的最優選的方法是什麼?
發帖時的例子確保編譯和運行 –
你是問你是否應該打破批成已知大小 – DaveH
這似乎很奇怪,該程序可以(在第二種情況下100?)在執行過程中關閉。如果程序結束是基於用戶交互的,請告訴用戶他尚未完成,並通知他後果。這樣用戶可以決定他是否想要丟失數據或想要等待。 –