关于Spring的JdbcTemplate批量更新batchUpdate()方法的返回值为-2的异常
近日在使用JdbcTemplate做大量的数据库数据同步的工作,基本就是“从A库查询数据--生成CSV--解析成SQL插入B库”这样一个过程。因为使用的是JdbcTemplate,所以在将数据导入B库时采用的是“先批量删,再批量插入”的策略。如果B库有相同主键的记录,则会先删掉,然后再将数据批量插入,这样就避免了主键冲突异常。这样更新数据量=删除记录数,新增数据量=总数据量-更新数据量。
但是在使用batchUpdate()函数时遇到了执行结果为-2的问题,先查查API:
batchUpdatepublic int[] batchUpdate(String[] sql) throws DataAccessExceptionDescription copied from interface: JdbcOperationsIssue multiple SQL updates on a single JDBC Statement using batching.Will fall back to separate updates on a single Statement if the JDBC driver does not support batch updates.Specified by:batchUpdate in interface JdbcOperationsParameters:sql - defining an array of SQL statements that will be executed.Returns:[b]an array of the number of rows affected by each statement[/b]Throws:DataAccessException - if there is any problem executing the batch