BatchedTooManyRowsAffectedException Batch update returned unexpected row count异常问题

hibernate+mysql,做多对多级联保存或更新时出现类似异常提示:Batch update returned unexpected row count from update actual row count

排除代码错误,最终找到问题是jdbc连接里加了参数rewriteBatchedStatements=true

这个参数是用于优化批处理的,因为mysql的批处理其实还是单个执行的,所以加了这个参数.

以下是该参数的mysql官方说明:

rewriteBatchedStatements

Should the driver use multiqueries (irregardless of the setting of "allowMultiQueries") as well as rewriting of prepared statements for INSERT into multi-value inserts when executeBatch() is called? Notice that this has the potential for SQL injection if using plain java.sql.Statements and your code doesn't sanitize input correctly. Notice that for prepared statements, server-side prepared statements can not currently take advantage of this rewrite option, and that if you don't specify stream lengths when using PreparedStatement.set*Stream(), the driver won't be able to determine the optimum number of parameters per batch and you might receive an error from the driver that the resultant packet is too large. Statement.getGeneratedKeys() for these rewritten statements only works when the entire batch includes INSERT statements. Please be aware using rewriteBatchedStatements=true with INSERT .. ON DUPLICATE KEY UPDATE that for rewritten statement server returns only one value as sum of all affected (or found) rows in batch and it isn't possible to map it correctly to initial statements; in this case driver returns 0 as a result of each batch statement if total count was 0, and the Statement.SUCCESS_NO_INFO as a result of each batch statement if total count was > 0.

你可能感兴趣的:(Java,数据库,hibernate)