I am writing a small piece of software that is supposed to insert records into the database used by a commercial application. The unique primary keys (ids) in the corresponding table (s) are sequential, but do not seem to be set to "auto increment". Thus, I assume that I will need to find the largest identifier, increment it and use this value for the record that I am inserting.
In pseudo code for brevity:
id = select max(id) from some_table
id++
insert into some_table values(id, othervalues...)
Now, if another thread started the same transaction before the first one completed its insertion, you will receive two identical identifiers and a failure when trying to insert the last one. You can verify this failure and try again, but a simpler solution would be to set the transaction isolation level. For this I need a SERIALIZABLE or lower level?
Also, is this usually a good way to solve a problem? Are there any other ways to do this?
source
share