Previous Topic: Retrieving a Bill-of-Materials StructureNext Topic: Implicit Record Locks


Locking Records

You can explicitly place a shared or exclusive lock on a record that is current of run unit, record, set, or area. You should place explicit locks on records for the following reasons:

To ensure later access, place a share lock on the record. To ensure exclusive access, place an exclusive lock on the record.

Steps in Locking Records

To place an explicit lock on a record, perform the following steps:

  1. Establish the appropriate run unit, record, set, or area currency.
  2. Issue the KEEP statement.
  3. Perform the IDMS-STATUS routine if the DBMS returns a nonzero value.

Alternatively, you can use the KEEP option of the FIND/OBTAIN statement to place locks on records as they are retrieved.

How Long Explicit Locks are Held

The DBMS maintains explicit record locks until the next COMMIT, FINISH, or ROLLBACK statement.

For more information on shared and exclusive locks, see Record Locks.

Example of Using KEEP to Lock a Record

The program excerpt below shows the use of the KEEP statement in a program that connects and disconnects records.

The program places an explicit shared lock on the new DEPARTMENT record occurrence to prevent other run units from modifying it and to guarantee access later in the program.

 A300-DISCONNECT-EMP.
     MOVE NEW-DEPT-ID-IN TO DEPT-ID-0410.
     FIND CALC DEPARTMENT.
*** IF ERROR-STATUS = 0326, NEW DEPT ID IS INVALID ***
     IF DB-REC-NOT-FOUND
         DISPLAY
        'NEW DEPARTMENT ' NEW-DEPT-ID-IN ' NOT FOUND'
        'FOR EMPLOYEE ID ' EMP-ID-IN
        GO TO A300-GET-NEXT
     ELSE IF DB-STATUS-OK
        NEXT SENTENCE
     ELSE
        PERFORM IDMS-STATUS.
*** LOCK NEW DEPARTMENT TO ENSURE THAT ***
*** OTHER RUN UNITS DO NOT MODIFY IT   ***
    KEEP CURRENT DEPARTMENT.
*** SAVE NEW DEPT DB-KEY TO REOBTAIN RECORD LATER ***
     MOVE DBKEY TO CONNECT-DBKEY.
     PERFORM IDMS-STATUS.

     MOVE OLD-DEPT-ID-IN TO DEPT-ID-0410.
     FIND CALC DEPARTMENT.
*** IF ERROR-STATUS = 0326, OLD DEPT ID IS INVALID ***
     IF DB-REC-NOT-FOUND
         DISPLAY
        'OLD DEPARTMENT ' OLD-DEPT-ID-IN ' NOT FOUND'
        'FOR EMPLOYEE ID ' EMP-ID-IN '
         GO TO A300-GET-NEXT
     ELSE IF DB-STATUS-OK
        NEXT SENTENCE
     ELSE
        PERFORM IDMS-STATUS.
     MOVE EMP-ID-IN TO EMP-ID-0415.
     OBTAIN CALC EMPLOYEE.
*** IF ERROR-STATUS = 0326, EMP ID IS INVALID ***
     IF DB-REC-NOT-FOUND
         DISPLAY
        'EMPLOYEE ' EMP-ID-IN ' NOT FOUND'
        'FOR OLD DEPARTMENT ' OLD-DEPT-ID-IN
        '*** NEW DEPARTMENT ' NEW-DEPT-ID-IN
        GO TO A300-GET-NEXT
     ELSE
        PERFORM IDMS-STATUS.