【问题标题】:Error Handling Spring JdbcTemplate batchUpdate错误处理 Spring JdbcTemplate batchUpdate
【发布时间】:2021-01-04 19:00:44
【问题描述】:

我正在尝试使用 batchUpdate 更新表中的数千行。我的要求是:

1) 假设一个批次有 1000 条记录。记录号 235 导致错误。如何找出导致错误的记录。

2) 假设记录 600 没有导致更新(原因可能是没有与 where 子句匹配的记录)。如何找出未导致更新的记录。

3) 在上述两种情况下,我该如何继续处理剩余的记录。

【问题讨论】:

    标签: spring error-handling spring-jdbc jdbctemplate


    【解决方案1】:

    经过长时间的搜索和调试,唯一的解决方案是转到 BatchUpdateException 类,找到负元素并从 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);
            }
    
        }
    
    }
    

    【讨论】:

    • 我看到你正在捕获 BatchUpdateException,但是当我尝试这样做时,我得到一个语法错误,因为它说 try 中的所有类都没有实际抛出它,文档证实了这一点。是什么引发了您的异常? (这就是你做 getCause() 的原因吗?)
    • @halfer 我也尝试过这样做,但我没有收到批量更新异常,你能帮我吗stackoverflow.com/questions/65561067/…
    • @Sameer:我是这个答案的编辑 - 我认为你的查询是 question_maven_com。但是,他们已经有 11 个月没有登录了。
    【解决方案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){
             
               
            }       
        }
    

    【讨论】:

    • 欢迎来到 Stack Overflow!您的答案仅包含(格式错误的)代码,而缺少某种解释。我建议阅读How do I write a good answer,然后也可以编辑答案。
    • 这似乎是一个不完整的答案,因此比 LQ 更糟糕。我试图为此举起 NAA 旗帜,但被拒绝了。
    猜你喜欢
    • 2012-04-06
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    相关资源
    最近更新 更多