First up, you won't be able to prevent users reading with UNCOMMITTED READ. To stop others reading as well as writing you will need an exclusive row lock. In theory this can be done in 3 ways:
- SELECT COL2 INTO val1 FROM TAB1 WHERE COL1 = 777 WITH RS USE AND KEEP EXCLUSIVE LOCKS
- UPDATE TAB1 SET COL2 = COL2 WHERE COL1 = 777
- UPDATE TAB1 SET COL2 = COL2+1 WHERE COL1 = 777
Interestingly although these all take and hold X row locks, only the last of these actually blocks reads; seems DB2 is trying to be clever and realising that the data isn't actually changing and reading through the X lock. So you might want to do the last option before selecting the data and then undoing the change with the real value after processing.
I should also have mentioned the traditional approach:
- DECLARE CURSOR X FOR
-
SELECT COL2 from TAB1 where COL1 = 777 FOR UPDATE OF COL2;
-
OPEN CURSOR X;
-
FETCH X INTO val1;
-
...process val1
-
UPDATE X SET COL2=val1 WHERE CURRENT OF X;
-
CLOSE X;
-
Used to be the only way to do this in the past but a lot more code change for you.