以下代码使用JdbcTemplate.batchUpdate()将行插入数据库;
Map<String,Object>[] batchValues = ...;
namedParameterJdbcTemplate.batchUpdate(sql, batchValues);
当它失败时,将引发一个DataAccessException。无论如何,我看不出哪些行(即地图中的条目)是有问题的,并导致了异常。当前,当引发这样的异常时,我们只是尝试独立地插入每一行。有没有更好的方法?谢谢
发布于 2013-09-30 02:03:19
我认为有两种可能性,并应根据要求作出决定。
发布于 2020-08-13 19:59:51
你可以得到失败的批次如下..。
try {
this.jdbCtemplate.batchUpdate(QueryConstant.SAVE_USER_NOTIFICATION, batchValues.toArray(new Map[usersResponse.size()]));
} catch (DataAccessException e) {
int[] updateCounts = ((BatchUpdateException) e.getCause()).getUpdateCounts();
int count = 1;
for (int i : updateCounts) {
if (i == Statement.EXECUTE_FAILED) {
failedBatch.add(count);
}
count++;
}
logger.info("Batch {} failed out of {} ",failedBatch, usersResponse.size()); }
https://stackoverflow.com/questions/19089624
复制相似问题