问题
I am using the below code to do the same. Whenever an sql error occurs(eg: number out of range) batch update stops and getting the result(int array) as null. so i added ignore
to the update
statement.
Now the batch is getting completed ignoring the sqlerror. This time I am getting failureids(ids skipped) and successids(ids processed as well as errors and exceptions). But i want the counts/ids of success,failures,errors,exceptions separately. how do I do that?
Also i added rewriteBatchedStatements=true
which helped gain so much time in inserting but updating still has performance issues. Updating 3000 records seems to take around 10 - 20 seconds. How do I overcome this?
Technologies used : spring, springjdbctemplate,mysql,intellij idea,java.
List<String> successList = new ArrayList<>();
List<String> failureList = new ArrayList<>();
int result[] = null;
int count = 0;
int successCount = 0;
int failCount = 0;
int notAavailable = 0;
int resultSetIndx = 0;
try {
result = jdbcTemplate.batchUpdate(
"update ignore xxx set yy = ?, zz = ? where aa = ?",
new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps,int i) throws SQLException {
ps.setDouble(1, Double.parseDouble(new JSONObject(jsonArray.get(i).toString()).get("aa").toString()));
ps.setDouble(2, Double.parseDouble(new JSONObject(jsonArray.get(i).toString()).get("bb").toString()));
ps.setString(3, new JSONObject(jsonArray.get(i).toString()).get("cc").toString());
}
public int getBatchSize() {
return jsonArray.length();
}
} );
for(int affectedRow : result){
if(affectedRow == 0){
failureList.add(new JSONObject(jsonArray.get(failCount+successCount).toString()).get("u_item_code").toString());
failCount++;
}else{
successList.add(new JSONObject(jsonArray.get(failCount+successCount).toString()).get("u_item_code").toString());
successCount++;
}
resultSetIndx++;
}
System.out.println("failCount = "+failCount);
System.out.println("successcount = "+successCount);
System.out.println(failureList);
System.out.println(successList);
}
catch (DataAccessException e){
Throwable rootCause = e.getRootCause();
System.out.println("Batch update failed: "+e);
if(rootCause instanceof BatchUpdateException){
try{
System.out.println("inside instance of batchupdateexception");
BatchUpdateException bue = (BatchUpdateException)rootCause;
int lastSuccessfullRow = bue.getUpdateCounts().length;
int failurePoint = lastSuccessfullRow+1;
int continuePoint = lastSuccessfullRow+2;
successCount+=lastSuccessfullRow;
failCount++;
System.out.println("Last successful row: "+lastSuccessfullRow);
System.out.println("Failed row: "+failurePoint);
System.out.println("continue point: "+continuePoint);
}catch(Exception exp){
System.out.println("Error occured at batch update"+exp.getMessage());
throw new Exception(exp);
}
}else{
//if rootcause is not BatchUpdateException, then re-throw the exception
throw new Exception(e);
}
}catch(Exception exp){
System.out.println("Exception occured"+exp);
throw new Exception(exp);
}
来源:https://stackoverflow.com/questions/50187244/how-to-get-errorsif-possible-exceptions-fails-success-counts-and-ids-from-jdb