Error Handling Spring JdbcTemplate batchUpdate

扶醉桌前 提交于 2021-01-21 09:29:49

问题


I am trying to update thousands of rows in a table using batchUpdate. My requirements are:

1) Assume there are 1000 records in a batch. Record No 235 caused an error. How do I find out which record caused the error.

2) Assume that record 600 did not result in an update (reason could be no record matching the where clause). How can I find out records that did not result in an update.

3) In both scenarios above how can I continue processing the remaining records.


回答1:


The only solution after long search and debug is to go to BatchUpdateException class and find the negative element and deduce the value of the insertion that is in error from the MAP.

import java.sql.BatchUpdateException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;


import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@Repository("dao_")
public class YouDao extends CommunDao implements IyouDao {

    public void bulkInsert(final List<Map<String, String>> map)
            throws BusinessException { 
        try {

            String sql = " insert into  your_table " + "(  aa,bb  )"
                    + "values " + "(  ?,? )";
            BatchPreparedStatementSetter batchPreparedStatementSetter = new BatchPreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement ps, int i)
                        throws SQLException {
                    Map<String, String> bean = map.get(i);

                    ps.setString(1, bean.get("aa"));
                    ps.setString(2, bean.get("bb")); 
                    //..
                    //..

                }

                @Override
                public int getBatchSize() {
                    return map.size();
                }
            };

             getJdbcTemplate().batchUpdate(sql, batchPreparedStatementSetter);

        }

        catch (Exception e) {
            if (e.getCause() instanceof BatchUpdateException) {
                BatchUpdateException be = (BatchUpdateException) e.getCause();
                int[] batchRes = be.getUpdateCounts();
                if (batchRes != null && batchRes.length > 0) {
                    for (int index = 0; index < batchRes.length; index++) {
                        if (batchRes[index] == Statement.EXECUTE_FAILED) {
                            logger.error("Error execution >>>>>>>>>>>"
                                    + index + " --- , codeFail : " + batchRes[index]
                                    + "---, line " + map.get(index));
                        }
                    }
                }
            }  
            throw new BusinessException(e);
        }

    }

}



回答2:


int[] rows =jdbcTemplate.batchUpdate(TbCareQueryConstant.SQL_UPDATE_BANKDETAILS_OF_USER, new BatchPreparedStatementSetter(){

.....
your code


}

 for(int i=0 ; i < rows.length ; i++){
        
     if(rows[i] == 0){
         
           
        }       
    }


来源:https://stackoverflow.com/questions/38615358/error-handling-spring-jdbctemplate-batchupdate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!