Using getGeneratedKeys with batch inserts in MySQL with Connector/J

后端 未结 1 1290
小蘑菇
小蘑菇 2021-01-04 02:35

Using Connector/J, I would like to do a batch insert into a master table followed by a batch insert into a details table (PreparedStatement.executeBatch() for b

相关标签:
1条回答
  • 2021-01-04 02:37

    Well, I ran some tests. With Connector/J 5.1 and MySQL 5.1.42, I observe the following:

    1. Statement.getGeneratedKeys() works as expected for inserts

    2. If a row was inserted or updated (the update count array returned by executeBatch() returns '1' or '2'), Statement.getGeneratedKeys() will have the key for that row. If the row was not modified (insert ignore or insert ... on duplicate key update that results in a no-op, executeBatch() returns 3), there is no key.

    3. The ResultSet returned by getGeneratedKeys will have the entries for successfully inserted rows, as per (2). There will not be a generated key row for the failed inserts (where update count value is Statement.EXECUTE_FAILED)

    4. ?

    5. Be careful with rewriteBatchedStatements in the JDBC connection string. If it is set to true, any failures will result in every row in the rewritten "chunk" to be treated as though it had failed. One way to handle this is to iterate the failed rows and retry them without batching.

    6. ?

    0 讨论(0)
提交回复
热议问题