Previous Topic: &VARTABLE Manipulation Facilities

Next Topic: Retrieval Techniques

Shared Table Updating

Tables allocated with a scope of REGION or SYSTEM can be accessed by any number of concurrently running NCL procedures. An NCL procedure can be interrupted at any time and another procedure can be scheduled. If two procedures are manipulating the same table, the possibility of logical corruption exists.

Example: Shared Table Updating

In this example, assume that the entry with key KEY001 has a data content of 10 before the code is executed.

procedure 1 procedure 2

&K = KEY001
&VARTABLE GET ID=T1 +
            SCOPE=SYSTEM +
            KEY=K FIELDS=D VARS=DATA
&D = &D + 1  -* &D now 11
-*
-* system interrupts procedure 1, schedules procedure 2
-*

                     &K = KEY001
                     &VARTABLE GET ID=T1 +
                                 SCOPE=SYSTEM +
                                 KEY=K FIELDS=D VARS=DATA
                     &D = &D + 1  -* &D now 11
                     &VARTABLE PUT ID=T1 +
                                 SCOPE=SYSTEM +
                                 KEY=K FIELDS=D VARS=DATA
                     &END
-*
-* system re-schedules procedure 1
-*
&VARTABLE PUT ID=T1 +
            SCOPE=SYSTEM +
            KEY=K FIELDS=D VARS=DATA

If the data in the table entry was being used as a counter, instead of having 12 (because of 2 adds), it only has 11, as the update done by procedure 2 is lost.

There are three solutions to the problem:

Example: Shared Table

&K = KEY001
.RETRY     -* loop here if update fails on correlator
           -* mismatch

&VARTABLE GET ID=T1 SCOPE=SYSTEM KEY=K +
        FIELDS=(.DATA,.USERCORR) VARS=(D,UC)
           -*
           -* manipulate the entry data (in &D).
           -*
&VARTABLE PUT ID=T1 SCOPE=SYSTEM KEY=K +
        FIELDS=(.DATA,.USERCORR) VARS=(D,UC)

&IF &ZFDBK = 8 &THEN &GOTO .RETRY-* loop if
                                 -* changed

Note: Because no locks are held, there is no extra work to release those locks should the procedure encounter an error condition while manipulating the data.

Also, if the GET is using an OPT= that retrieves a different key value record (for example OPT=GEN), you do not need to know in advance what key value to lock. If &LOCK had to be used in this case, you might need to lock the entire table.

Note: The use of the user correlator can be forced, for a particular table, by specifying USERCORR=YES when allocating it.

(To contrast these two views, (2) can be regarded as the conservative approach, and (3) can be regarded as the aggressive approach.)