Previous Topic: Maintaining Data Integrity in the Online EnvironmentNext Topic: Monitoring Concurrent Database Access


Setting Longterm Explicit Locks

In pseudoconversational programming, you may be required to lock records across run units for the duration of a transaction. For example, a high-priority update application may lock record occurrences as they are retrieved in order to prevent other run units from accessing data that is about to be modified.

Steps to Set Longterm Locks

To lock a database record explicitly across a pseudoconverse, perform the following steps:

  1. Retrieve the database record.
  2. Issue a KEEP LONGTERM statement that specifies either the SHARE CURRENT or the EXCLUSIVE CURRENT parameter:
  3. Perform pseudoconversational processing, as required.
  4. As soon as possible, release the explicit lock by issuing a KEEP LONGTERM statement with the RELEASE parameter.

Important! Release longterm locks as soon as possible to provide availability to other run units.

Interaction of Longterm Locks

Locks in effect

Locks allowed for other run units

Locks disallowed for other run units

Shared

Shared and longterm shared

Exclusive and longterm exclusive

Exclusive

None

Shared, exclusive, longterm shared, and longterm exclusive

Longterm shared

For all run units: shared and longterm shared .sp For run units on the same terminal: exclusive and longterm exclusive

For run units on other terminals: exclusive and longterm exclusive

Longterm exclusive

For run units on the same terminal: shared, exclusive, longterm shared, and longterm exclusive

For run units on other terminals: shared, exclusive, longterm shared, and longterm exclusive

Example of Setting Longterm Exclusive Locks

The first program excerpt below sets longterm exclusive locks in order to ensure that other programs cannot access any data. (The second program excerpt performs database modifications and releases the locks as soon as possible.)


The first program excerpt locks the EMPLOYEE and DEPARTMENT records in order to prevent other run units from modifying them during the pseudoconverse.

 DATA DIVISION.
 WORKING-STORAGE SECTION.
 01  CHNGDEPT                   PIC X(8)   VALUE 'CHNGDEPT'.
 01  KEEP-INFO.
     05 DEPT-LNGTRM-ID          PIC X(4)   VALUE 'DEPT'.
     05 EMPL-LNGTRM-ID          PIC X(4)   VALUE 'EMPL'.
 01  MAP-WORK-REC.
     05  WORK-OLD-DEPT-ID       PIC 9(4).
     05  WORK-NEW-DEPT-ID       PIC 9(4).
     05  WORK-EMP-ID            PIC 9(4).
     05  WORK-FIRST             PIC X(10).
     05  WORK-LAST              PIC X(15).
     05  WORK-ADDRESS           PIC X(42).
 PROCEDURE DIVISION.
     BIND MAP DCTEST03.
     BIND MAP DCTEST03 RECORD MAP-WORK-REC.
     MAP IN USING DCTEST03.
     MOVE WORK-EMP-ID TO EMP-ID-0415.
     OBTAIN CALC EMPLOYEE
        ON DB-REC-NOT-FOUND GO TO ERR-NO-EMP.
*** SET AN EXCLUSIVE LOCK ON THE CURRENT EMPLOYEE RECORD ***
     KEEP LONGTERM EMPL-LNGTRM-ID
         EXCLUSIVE CURRENT EMPLOYEE.
     MOVE EMP-ID-0415 TO WORK-EMP-ID.
     MOVE EMP-LAST-NAME-0415 TO WORK-LAST.
     MOVE EMP-FIRST-NAME-0415 TO WORK-FIRST.
     MOVE EMP-ADDRESS-0415 TO WORK-ADDRESS.
     IF DEPT-EMPLOYEE IS NOT EMPTY
        OBTAIN OWNER IN DEPT-EMPLOYEE
     ELSE GO TO NO-DEPT.
*** SET AN EXCLUSIVE LOCK ON THE CURRENT DEPARTMENT RECORD ***
     KEEP LONGTERM DEPT-LNGTRM-ID
         EXCLUSIVE CURRENT DEPARTMENT.
     MOVE DEPT-ID-0410 TO WORK-OLD-DEPT-ID.
*** ALLOW INPUT IN THE NEW DEPARTMENT FIELD ONLY ***
     MODIFY MAP DCTEST03 FOR ALL EXCEPT
           DFLD WORK-NEW-DEPT-ID
           ATTRIBUTES PROTECTED.
     MAP OUT USING DCTEST03.
     DC RETURN NEXT TASK CODE CHNGDEPT.

Example of Releasing Longterm Exclusive Locks

This program excerpt maps in the new department ID, disconnects the employee from the old department, and connects the record to the new department.

 DATA DIVISION.
 WORKING-STORAGE SECTION.
 01  CHNGSHOW                   PIC X(8)   VALUE 'CHNGSHOW'.
 01  TEMP-DEPT-DBKEY            PIC S9(8) COMP.
 01  KEEP-INFO.
     05 DEPT-LNGTRM-ID          PIC X(4)   VALUE 'DEPT'.
     05 EMPL-LNGTRM-ID          PIC X(4)   VALUE 'EMPL'.
 01  MAP-WORK-REC.
     05  WORK-OLD-DEPT-ID       PIC 9(4).
     05  WORK-NEW-DEPT-ID       PIC 9(4).
     05  WORK-EMP-ID            PIC 9(4).
     05  WORK-FIRST             PIC X(10).
     05  WORK-LAST              PIC X(15).
     05  WORK-ADDRESS           PIC X(42).
 PROCEDURE DIVISION.
     BIND MAP DCTEST03.
     BIND MAP DCTEST03 RECORD MAP-WORK-REC.
     MAP IN USING DCTEST03.
     IF WORK-NEW-DEPT-ID IS NOT NUMERIC
         GO TO ERR-NONNUMERIC-DEPT-ID.
*** OBTAIN NEW DEPARTMENT RECORD TO ENSURE IT EXISTS ***
     MOVE WORK-NEW-DEPT-ID TO DEPT-ID-0410.
     FIND CALC DEPARTMENT
         ON DB-REC-NOT-FOUND GO TO ERR-NO-NEW-DEPT.
     MOVE DBKEY TO TEMP-DEPT-DBKEY.
*** REOBTAIN OLD DEPARTMENT ***
     MOVE WORK-OLD-DEPT-ID TO DEPT-ID-0410.
     FIND CALC DEPARTMENT.
*** REOBTAIN EMPLOYEE RECORD ***
     MOVE WORK-EMP-ID TO EMP-ID-0415.
     FIND CALC EMPLOYEE.
     DISCONNECT EMPLOYEE FROM DEPT-EMPLOYEE.
*** REOBTAIN NEW DEPARTMENT USING SAVED DB-KEY ***
     FIND DEPARTMENT USING TEMP-DEPT-DBKEY.
     CONNECT EMPLOYEE TO DEPT-EMPLOYEE.
*** RELEASE ALL LONGTERM LOCKS ***
     KEEP LONGTERM ALL RELEASE.
     MAP OUT USING DCTEST03 OUTPUT DATA IS ATTRIBUTE
         MESSAGE IS EMP-CONNECTED-MESS LENGTH 80.
     DC RETURN NEXT TASK CODE CHNGSHOW.