I’m trying to increment a column’s value and return it to the caller in a manner that is thread-safe and although I’ve seen many proposed solutions to this in the forums I haven’t seen one that will ensure two distinct clients of the database won’t get the same result. The table that I’m going to be creating will look like:
Column Name Data Type
----------- --------------
Description varchar(50)
CurrentValue int
Data in the table will look like:
Description CurrentValue
---------------- ---------------------
Billing 123422
Policy 28383
So with that information available, how do I in a thread-safe manner (without race conditions) increment the value in the CurrentValue column and return it to the user who caused it to be incremented?
I’m pretty sure the platform I’ll be running on is DB2 and the clients will be Java applications.
Database sequences (example in Oracle DBMS) should take care of this in PostgreSQL, MS SQL and Oracle, at least. Your column with the incremental number should therefore be an
IDcolumn (or similar in principle) that reads its value from the sequence to which it is bound.Each time a
SELECT(i.e. a read) occurs on the sequence it’s value is incremented, whatever you do with that value later on.