Hi all!
I'm currently trying to generate my primary keys based on a table that is held in my DB. For example, I have a table that has a table (PRIMARY_KEY) that consists of columns "TABLE_NAME" and "PRIMARY_KEY_VALUE". An example of a record that might exist in this table is:
TABLE_NAME, PRIMARY_KEY_VALUE
RESOURCE, 1000
What I want to do when I insert a new record into the RESOURCE table is to pull the next value for the primary key of RESOURCE out of my PRIMARY_KEY table. So, in the example above, I want to first get the current value (1000), increment it by one, replace the new value (1001), and then use this new value as the primary key for the Object I'm inserting into the RESOURCE table.
My problem comes from trying to get a WRITE lock on the record before I actually update it. A READ lock isn't good enough because there will be an issue of someone reading the same record between the time that I read it and re-write it. I looked into the UPGRADE lock, but my application needs to be database-independent, so that isn't really an option. Does anybody have any ideas on how I can successfully accomplish this? BTW - unfortunately, changing the primary key strategy is not an option.
Thanks in advance for any help that anyone can offer!
|