How to identify the lock acquired by another user on a record in progress openedge

只谈情不闲聊 提交于 2019-12-12 01:51:46

问题


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:


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.



回答2:


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!




回答3:


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.


来源:https://stackoverflow.com/questions/28882506/how-to-identify-the-lock-acquired-by-another-user-on-a-record-in-progress-opened

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!