Java executeBatch异常处理

Java executeBatch异常处理,java,sql,jdbc,Java,Sql,Jdbc,executeBatch()将引发两个异常SQLException和BatchUpdateException。需要有关如何处理此异常的帮助 我们正在为分布式环境执行900个批处理限制和150个提交限制。仅处理唯一的BatchUpdateException。下面是代码片段。有人能指导我们是否需要捕获SQLException吗。如果未捕获SQLException,结果是什么 抛出: SQLException-如果发生数据库访问错误,则对关闭语句调用此方法,或者驱动程序不支持批处理语句。--这句话的意

executeBatch()将引发两个异常SQLException和BatchUpdateException。需要有关如何处理此异常的帮助

我们正在为分布式环境执行900个批处理限制和150个提交限制。仅处理唯一的BatchUpdateException。下面是代码片段。有人能指导我们是否需要捕获SQLException吗。如果未捕获SQLException,结果是什么

抛出: SQLException-如果发生数据库访问错误,则对关闭语句调用此方法,或者驱动程序不支持批处理语句。--这句话的意思是什么

} catch (BatchUpdateException be) {

    status = "failure";
    errorDesc = "BatchUpdateException";
    errorCode = "-1";
    checkpoint = "12";
    log.debug(DealerId + "->BatchException be:" + be + ", checkpoint: " + checkpoint
            + ",con: " + con + ",db2con: " + db2Con);

    int[] updateStatus = be.getUpdateCounts();

    if (updateStatus != null) {
        ArrayList<String> failedList = new ArrayList<String>();
        try {
            verifyUpdateCount(updateStatus, failedList, batchQueryList, queryList);

        } catch (Exception e) {
            if (failedList.size() > 0) {
                log.debug(DealerId + "->updateStatus Exception queryList size:"
                        + queryList.size());
                queryList.clear();
                queryList.addAll(failedList);
            }
            log.debug(DealerId + "->updateStatus Exception e:" + e + ", checkpoint: "
                    + checkpoint + "con: " + con + "db2con: " + db2Con);
        }
    }
    break END;
} finally {
    try {
        log.debug("norecords:" + noRecords);
        if (!noRecords) {
            closeStatements(ps, rs, rsShdw, db2Ps, stShdw);
        }
    } catch (Exception e) {
        printCheckpoint(queryList, con, db2Con, "closeStatements Exception e: ", e,
                checkpoint);
    }
}
}捕获(BatchUpdateException be){
status=“失败”;
errorDesc=“BatchUpdateException”;
errorCode=“-1”;
检查点=“12”;
log.debug(DealerId+“->BatchException be:“+be+”,检查点:“+checkpoint
+,con:“+con+”,db2con:“+db2con”);
int[]updateStatus=be.getUpdateCounts();
if(updateStatus!=null){
ArrayList failedList=新建ArrayList();
试一试{
verifyUpdateCount(updateStatus、failedList、batchQueryList、queryList);
}捕获(例外e){
如果(failedList.size()>0){
log.debug(DealerId+“->updateStatus异常查询列表大小:”
+queryList.size());
queryList.clear();
queryList.addAll(失败列表);
}
log.debug(DealerId+”->updateStatus异常e:“+e+”,检查点:”
+检查点+“con:+con+”db2con:+db2con);
}
}
断端;
}最后{
试一试{
log.debug(“norecords:+norecords”);
如果(!noRecords){
关闭语句(ps、rs、rsShdw、db2Ps、stShdw);
}
}捕获(例外e){
printCheckpoint(queryList,con,db2Con,“closeStatements异常e:”,e,
检查点);
}
}
Throws:SQLException-如果发生数据库访问错误,则对关闭语句调用此方法,或者驱动程序不支持批处理语句。--这句话的意思是什么

} catch (BatchUpdateException be) {

    status = "failure";
    errorDesc = "BatchUpdateException";
    errorCode = "-1";
    checkpoint = "12";
    log.debug(DealerId + "->BatchException be:" + be + ", checkpoint: " + checkpoint
            + ",con: " + con + ",db2con: " + db2Con);

    int[] updateStatus = be.getUpdateCounts();

    if (updateStatus != null) {
        ArrayList<String> failedList = new ArrayList<String>();
        try {
            verifyUpdateCount(updateStatus, failedList, batchQueryList, queryList);

        } catch (Exception e) {
            if (failedList.size() > 0) {
                log.debug(DealerId + "->updateStatus Exception queryList size:"
                        + queryList.size());
                queryList.clear();
                queryList.addAll(failedList);
            }
            log.debug(DealerId + "->updateStatus Exception e:" + e + ", checkpoint: "
                    + checkpoint + "con: " + con + "db2con: " + db2Con);
        }
    }
    break END;
} finally {
    try {
        log.debug("norecords:" + noRecords);
        if (!noRecords) {
            closeStatements(ps, rs, rsShdw, db2Ps, stShdw);
        }
    } catch (Exception e) {
        printCheckpoint(queryList, con, db2Con, "closeStatements Exception e: ", e,
                checkpoint);
    }
}
我不知道它是否能变得更简单。你不明白哪一部分

它表示该方法在以下情况之一时抛出
SQLException

  • 发生数据库访问错误
  • 在封闭语句上调用此方法
  • 驱动程序不支持批处理语句
BatchUpdateException被捕获,如果我们也捕获SQLException的话。这是必需的吗?
BatchUpdateException扩展了SQLException
,因此如果捕获后者,则不需要捕获前者,除非您希望以不同的方式对待它