0

User A has read first record from a customer table. User B wishes to know the lock which is held by A on the record. How will B identify the lock held by User A programmatically.

1
  • What does "wishes to know the lock is held" mean?
    – Tom Bascom
    Mar 5, 2015 at 18:56

3 Answers 3

1

You can use LOCKED function:

Returns a TRUE value if a record is not available to a prior FIND . . . NO-WAIT statement because another user has locked a record.

Example of the documentation:

REPEAT:
    PROMPT-FOR customer.cust-num.
    FIND customer USING customer.cust-num NO-ERROR NO-WAIT.
    IF NOT AVAILABLE customer THEN DO:
        IF LOCKED customer
        THEN MESSAGE "Customer record is locked".
        ELSE MESSAGE "Customer record was not found".
        NEXT.
    END.
    DISPLAY cust-num name city state.
END.

Other example:

FIND FIRST table
     WHERE table.c1 = "foo"
     EXCLUSIVE-LOCK NO-ERROR NO-WAIT.
IF LOCKED(table)   
THEN DO:
    /* The record is blocked by another user */
END. 
ELSE DO:
    IF AVAILABLE table 
    THEN DO:
        /* The record can be modified */
    END.
END.
4
  • how can i find whether it is Exclusive, Share or No-lock? Mar 5, 2015 at 16:26
  • With LOCKED function, you can know if the record is locked (with EXCLUSIVE-LOCK or SHARE-LOCK) or not (with NO-LOCK or SHARE-LOCK)
    – doydoy44
    Mar 5, 2015 at 16:37
  • Still it is not clear that whether it is exclusive, share or no-lock. Mar 5, 2015 at 16:58
  • If a record is modified by EXCLUSIVE-LOCK or SHAR-LOCK, the LOCKED function return TRUE otherwise, the function return FALSE. So you just can to know if the record is locked.
    – doydoy44
    Mar 5, 2015 at 17:04
1

You can use the built in _Lock Virtual System Table.

This is a basic example:

DEFINE TEMP-TABLE ttLock
  FIELD LockId LIKE _Lock._Lock-Id
  FIELD LockUsr LIKE _Lock._Lock-Usr
  FIELD LockName LIKE _Lock._Lock-Name
  FIELD LockTable LIKE _Lock._Lock-Table
  FIELD LockFlags LIKE _Lock._Lock-flags
  INDEX LockIdx IS PRIMARY UNIQUE LockId.

FOR EACH _Lock NO-LOCK:
  IF _Lock._Lock-Usr = ? THEN NEXT .
  CREATE ttLock.
  ASSIGN
      LockId    = _Lock._Lock-Id
      LockUsr   = _Lock._Lock-Usr
      LockName  = _Lock._Lock-Name
      LockTable = _Lock._Lock-Table
      lockFlags = _Lock._Lock-flags.
END.

FOR EACH ttlock:
  FIND _Trans NO-LOCK WHERE  _Trans._Trans-Usrnum = ttLock.LockUsr NO-ERROR.
  FIND _File NO-LOCK WHERE _File-Number = ttLock.LockTable.

  MESSAGE
   "Transaction Id:~t" (IF AVAILABLE _Trans THEN _Trans._Trans-Id ELSE ?) "~n"
   "User Number:~t" ttLock.LockUsr "~n"
   "User Name~t" ttLock.LockName "~n"
   "Table Number:~t" ttLock.LockTable "~n"
   "Table Name:~t" _File-Name "~n"
   "Flags:~t" ttLock.LockFlags
   VIEW-AS ALERT-BOX INFO BUTTONS OK.
END.

(Taken directly from this entry in the knowledgebase.)

In the Database Administration guide you can see (page 848) you can see what the flags in _Lock.LockFlags stand for.

Flags for the lock—the flags specify a share lock (S), exclusive lock (X), a lock upgraded from share to exclusive(U), a lock in limbo (L), a queued lock(Q), a lock kept across transaction end boundary (K), a lock is part of a JTAtransaction (J), a lock is in create mode for JTA (C), or a lockwait timeout has expired on this queued lock (E)

Querying the _Lock table might be something you want to do in test environments only. Depending on your systems size there might be LOT of data there. Also, use VSTs for READ-ONLY operations!

1
  • 2
    Querying _LOCK in production on releases previous to 11.4 is a very, very, very bad idea. 11.4 and above have changed the implementation to a "snapshot" which makes it less of a problem. But old code should not try to manipulate _lock.
    – Tom Bascom
    Mar 5, 2015 at 18:55
-1

Querying _LOCK in older releases is OK, but you have to use the appropriate code for the respective version:

  • _LOCK always has the full number of entries given in -L, regardless how many locks currently exist.
  • In pre-11.4 releases the fields are not indexed, but all used locks are at the beginning of the table, so you can use

    IF _Lock._Lock-Usr = ? THEN LEAVE.
    

    in the for each loop (_Lock._Lock-Name = ? is also fine). See http://knowledgebase.progress.com/articles/Article/P161995

  • In 11.4 and 11.5 the populated entries are no longer at the beginning so the old code will give wrong results (see http://knowledgebase.progress.com/articles/Article/000056304, this is fixed in 11.5.1). Fortunately scanning the lock table is now much faster so you can use

    FOR EACH _Lock NO-LOCK WHERE _Lock-Recid <> ?:
    

    mentioned in the same article. Technically this is not implemented with indices. (An index wouldn't work with the <> operator.)

  • In 11.5 and 11.6 both variants should work, but the newer variant with a where phrase should be faster.
1
  • It is not true that all locks are below the first one with _lock-usr = ?. It appears to be true on small test systems. On large production systems you will discover that this is not reliable.
    – Tom Bascom
    Jun 12, 2018 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.