Question

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:

  1. 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.

  2. Add an extra column with a fake id that is only valid during the transaction.

  3. 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?

Was it helpful?

Solution

Partial answer

Is pulling keys e.g. in Oracle also transaction save?

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.

OTHER TIPS

Never run into this, so I dived into it a little. First of all, there is a way to retrieve the generated ids from a JDBC statement:

String sql = "INSERT INTO AUTHORS (LAST, FIRST, HOME) VALUES " +
               "'PARKER', 'DOROTHY', 'USA', keyColumn";

int rows = stmt.executeUpdate(sql, 
               Statement.RETURN_GENERATED_KEYS);

ResultSet rs = stmt.getGeneratedKeys();
if (rs.next()) {
         ResultSetMetaData rsmd = rs.getMetaData();
         int colCount = rsmd.getColumnCount();
         do {
             for (int i = 1; i <= colCount; i++) {
                 String key = rs.getString(i);
                 System.out.println("key " + i + "is " + key);
             }
         }
         while (rs.next();)
} 
else {
         System.out.println("There are no generated keys.");
}

see this http://download.oracle.com/javase/1.4.2/docs/guide/jdbc/getstart/statement.html#1000569

Also, theoretically it could be combined with the JDBC batchUpdate

Although, this combination seems to be rather non-trivial, on this pls refer to this thread. I sugest to try this, and if you do not succeed, fall back to pre-fetching from sequence.

getAutogeneratedKeys() will also work with a batch update as far as I remember.

It returns a ResultSet with all newly created ids - not just a single value.

But that requires that the ID is populated through a trigger during the INSERT operation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top