java - Using getGeneratedKeys with batch inserts in MySQL with Connector/J -
using connector/j, batch insert master table followed batch insert details table (preparedstatement.executebatch()
both). haven't found information online, i'm looking feedback people have experience this.
can use
statement.getgeneratedkeys()
ids of newly inserted rows in master table can use them foreign keys in detail inserts?what if not every query resulted in insert (e.g. there
insert ignore
orinsert ... on duplicate key update
query)? row instatement.getgeneratedkeys()
every statement, or new ones?what
statement.getgeneratedkeys()
return there error 1 of inserted master records, ,continuebatchonerror
settrue
in connection string?are there differences in related behavior between connector/j versions 5.0.x vs 5.5.x? mysql 5.0 vs 5.1?
any there other issues or gotchas should aware of?
is there better way this?
well, ran tests. connector/j 5.1 , mysql 5.1.42, observe following:
statement.getgeneratedkeys()
works expected insertsif row inserted or updated (the update count array returned
executebatch()
returns '1' or '2'),statement.getgeneratedkeys()
have key row. if row not modified (insert ignore
orinsert ... on duplicate key update
results in no-op,executebatch()
returns3
), there no key.the resultset returned
getgeneratedkeys
have entries inserted rows, per (2). there not generated key row failed inserts (where update count valuestatement.execute_failed
)?
be careful
rewritebatchedstatements
in jdbc connection string. if settrue
, failures result in every row in rewritten "chunk" treated though had failed. 1 way handle iterate failed rows , retry them without batching.?
Comments
Post a Comment