在用jdbc对oracle数据库执行批量提交数据操作时,我们会采用batch的方式进行批量提交
batch ,如果需要批量更新100条,如果在第31条处出现了异常,那么之后的数据就不会更新了,如果加上事务管理,那么只要有出错了,所有的batch执行的都会回滚
private synchronized void execData() throws Exception{
if(data.size()==0){
return ;
}
PreparedStatement pstmt = null;
try {
conn.setAutoCommit(false);
Enumeration en=data.keys();
while (en.hasMoreElements()) {
Integer i=(Integer)en.nextElement();
pstmt = conn.prepareStatement((String)data.get(i));
pstmt.addBatch();
dv.add(i);
}
pstmt.executeBatch();
conn.commit();
} catch (BatchUpdateException e) {//batch异常时需要单条处理,并且需要回滚之前的操作
e.printStackTrace(System.out);
try {
conn.rollback();
} catch (Exception exception) {
// TODO: handle exception
exception.printStackTrace();
}
singleExec();
return;
}catch (Exception e) {//其他异常
e.printStackTrace(System.out);
dv.clear();
return;
}finally {//关闭资源
try {
pstmt.close();
} catch (Exception e) {
e.printStackTrace(System.out);
}
try {
conn.close();
} catch (Exception e) {
// TODO: handle exception
throw new Exception("操作出现异常") ;
}
}
try{
Enumeration en=dv.elements();
while (en.hasMoreElements()) {
Integer i=(Integer)en.nextElement();
data.remove(i);
}
dv.clear();
}catch (Exception e) {
e.printStackTrace(System.out);
}
}