Subject | Atomic Increment/Decrement |
---|---|
Author | Robert DiFalco |
Post date | 2003-06-01T16:38:08Z |
I would like to do something like this:
UPDATE Child SET Child.parentCount = Child.parentCount + 1 WHERE
Child.id = :childId;
Because I was to increment (or sometimes decrement) the parentCount
column atomically. Is this possible, or do I need to first perform a
query such as:
SELECT Child.parentCount FROM Child WHERE Child.id = :childId
INTO :parentCount;
UPDATE Child SET Child.parentCount = :parentCount + 1 WHERE
Child.id = :childId;
Doing this isn't atomic since two concurrent operations could read
pre-incremented values (i.e. the second operation starts in between the
SELECT and UPDATE). I guess my first guess at a solution is:
// locks the record? Is it locked for reading?
UPDATE Child SET Child.parentCount = Child.parentCount WHERE
Child.id = :childId;
SELECT Child.parentCount FROM Child WHERE Child.id = :childId
INTO :parentCount;
UPDATE Child SET Child.parentCount = :parentCount + 1 WHERE
Child.id = :childId;
Is there some other way to perform an atomic increment/decrement of a
table column?
Tia!
R.
UPDATE Child SET Child.parentCount = Child.parentCount + 1 WHERE
Child.id = :childId;
Because I was to increment (or sometimes decrement) the parentCount
column atomically. Is this possible, or do I need to first perform a
query such as:
SELECT Child.parentCount FROM Child WHERE Child.id = :childId
INTO :parentCount;
UPDATE Child SET Child.parentCount = :parentCount + 1 WHERE
Child.id = :childId;
Doing this isn't atomic since two concurrent operations could read
pre-incremented values (i.e. the second operation starts in between the
SELECT and UPDATE). I guess my first guess at a solution is:
// locks the record? Is it locked for reading?
UPDATE Child SET Child.parentCount = Child.parentCount WHERE
Child.id = :childId;
SELECT Child.parentCount FROM Child WHERE Child.id = :childId
INTO :parentCount;
UPDATE Child SET Child.parentCount = :parentCount + 1 WHERE
Child.id = :childId;
Is there some other way to perform an atomic increment/decrement of a
table column?
Tia!
R.