我试图找出使用 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);
}
哪种方法是最好的方法?