public PreparedStatement prepareStatement(String sql,
int autoGeneratedKeys)
throws SQLException
Parameters:
sql - an SQL statement that may contain one or more '?' IN parameter placeholders
autoGeneratedKeys - a flag indicating whether auto-generated keys should be returned; one of Statement.RETURN_GENERATED_KEYS or Statement.NO_GENERATED_KEYS
Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Yes. No two records will receive the same key.Just one more question... is the use of autogenerated keys thread-safe?
No. The key generation algorithm is database dependent. Many databases don't guarantee that the keys are given in sequence, without gaps.In other words, what happens if 2 threads run the method containing insert/get generated key at the same time? I want to avoid interleaving
OCUP UML fundamental and ITIL foundation
How do you read those keys? If done properly, this should not be an issue. As long as you don't select the max value for that column to get the generated keys, you will be fine. Look for Statement.getGeneratedKeys().I want to avoid that the two threads read the same key, while the ids of the rows they inserted are obviously different!
OCUP UML fundamental and ITIL foundation