Kbase 21500: 4GL/ABL: How to Implement the Optimistic Locking Strategy to manage record locking in a Multi-User E
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  20/11/2009 |
|
Status: Verified
GOAL:
4GL/ABL: How to Implement the Optimistic Locking Strategy to manage record locking in a Multi-User Environment?
GOAL:
How is the FIND CURRENT statement used in the implementation of the Optimistic Locking Strategy?
GOAL:
How are the AVAILABLE, LOCKED and CURRENT-CHANGED functions used in the implementation of the Optimistic Locking Strategy?
GOAL:
Sample code implementing the 'Optimistic Locking Strategy' to improve concurrency and eliminate record contention errors in a Multi-User Environment.
FACT(s) (Environment):
Progress 8.X
Progress 9.X
OpenEdge 10.x
All Supported Operating Systems
OpenEdge Category: Language (4GL/ABL)
FIX:
When developing a 4GL application for deployment in a multi-user environment, you will want to design a record locking strategy that avoids keeping records locked unnecessarily for long periods of time. The Optimistic Locking Strategy is recommended to accomplish this objective. Optimistic Locking is a strategy whereby records are initially accessed using NO-LOCK, with an EXCLUSIVE-LOCK used afterwards for a very short period of time.
This example runs 2 sessions against the Sports2000 Data Base.
In the first session, procedure 1 accesses a record with EXCLUSIVE-LOCK and procedure 2 accesses the same record using Optimistic Locking Strategy. In the second procedure, notice the use of the NO-LOCK NO-ERROR NO-WAIT options of the first FIND and the use of the FIND CURRENT EXCLUSIVE-LOCK NO-ERROR NO-WAIT of the first FIND. Also, notice the roles played by the AVAILABLE, LOCKED and CURRENT-CHANGED functions in the implementation of this Optimistic Locking Strategy:
/* procedure 1 */
PROMPT-FOR customer.custnum.
FIND customer USING INPUT customer.custnum
EXCLUSIVE-LOCK NO-ERROR NO-WAIT.
IF AVAILABLE customer THEN DO:
DISPLAY Customer.custnum customer.NAME WITH 1 COLUMN.
UPDATE customer.NAME.
END.
PAUSE.
/* procedure 2 -- Optimistic Locking Example */
PROMPT-FOR customer.custnum.
FIND customer USING INPUT customer.custnum NO-LOCK NO-ERROR NO-WAIT.
IF AVAILABLE customer THEN
DISPLAY Customer.custnum customer.NAME WITH 1 COLUMN.
PAUSE.
MESSAGE "Do you wish to change this record?"
VIEW-AS ALERT-BOX QUESTION BUTTONS YES-NO UPDATE upd AS LOGICAL.
IF upd THEN DO:
FIND CURRENT customer EXCLUSIVE-LOCK NO-ERROR NO-WAIT.
IF AVAILABLE customer THEN DO:
IF CURRENT-CHANGED customer THEN DO:
DISPLAY Customer.custnum customer.NAME WITH 1 COLUMN.
UPDATE customer.NAME.
END.
ELSE DO:
UPDATE customer.NAME.
END.
END.
ELSE IF NOT AVAILABLE customer THEN DO:
IF LOCKED customer THEN DO:
MESSAGE "This Record is Locked by another user"
VIEW-AS ALERT-BOX.
END.
ELSE DO:
MESSAGE "This record has been deleted" VIEW-AS ALERT-BOX.
END.
END.
END.