Concurrency issues
EXEC SQL DECLARE sto_curse CURSOR FOR
SELECT * FROM stock
WHERE manu_code = 'ANZ';
EXEC SQL UPDATE stock
SET unit_price = 1.15 * unit_price
WHERE manu_code = 'ANZ';
- The other program finishes its update before your program fetches
its first row.
Your program shows you only updated rows.
- Your program fetches every row before the other program has a
chance to update it.
Your program shows you only original rows.
- After your program fetches some original rows, the other program
catches up and goes on to update some rows that your program has yet
to read; then it executes the COMMIT WORK statement.
Your program might return a mixture of original rows and updated rows.
- Same as number 3, except that after updating
the table, the other program issues a ROLLBACK WORK statement.
Your program can show you a mixture of original rows and updated rows that no longer exist in the database.
The first two possibilities are harmless. In possibility number 1, the update is complete before your query begins. It makes no difference whether the update finished a microsecond ago or a week ago.
In possibility number 2, your query is, in effect, complete before the update begins. The other program might have been working just one row behind yours, or it might not start until tomorrow night; it does not matter.
The last two possibilities, however, can be important to the design of some applications. In possibility number 3, the query returns a mix of updated and original data. That result can be detrimental in some applications. In others, such as one that is taking an average of all prices, it might not matter at all.
Possibility number 4 can be disastrous if a program returns some rows of data that, because their transaction was cancelled, can no longer be found in the table.
- Your program fetches the row.
- Another program updates or deletes the row.
- Your program updates or deletes WHERE CURRENT OF cursor_name.
To control concurrent events such as these, use the locking and isolation level features of the database server.