I insert alot of data into a table with a autogenerated key using the batchUpdate functionality of JDBC. Because JDBC doesn’t say anything about batchUpdate and getAutogeneratedKeys I need some database independant workaround.
My ideas:
-
Somehow pull the next handed out sequences from the database before inserting and then using the keys manually. But JDBC hasn’t got a
getTheNextFutureKeys(howMany). So how can this be done? Is pulling keys e.g. in Oracle also transaction save? So only one transaction can ever pull the same set of future keys. -
Add an extra column with a fake id that is only valid during the transaction.
-
Use all the other columns as secondary key to fetch the generated key. This isn’t really 3NF conform…
Are there better ideas or how can I use idea 1 in a generalized way?
Partial answer
Yes, getting values from a sequence is transaction safe, by which I mean even if you roll back your transaction, a sequence value returned by the DB won’t be returned again under any circumstances.
So you can prefetch the id-s from a sequence and use them in the batch insert.