Kbase P16005: How to Implement an Optimistic Locking Strategy?
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  11/29/2003 |
|
Status: Unverified
GOAL:
How to Implement an Optimistic Locking Strategy?
GOAL:
KB21500
FACT(s) (Environment):
Progress 8.x
FACT(s) (Environment):
Progress 9.x
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 Database.
In the first session, procedure 1 accesses a record with
EXCLUSIVE-LOCK. The second session will perform the same process
using Optimistic Locking Strategy instead.
/* 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 no-LOCK NO-ERROR NO-WAIT.
IF AVAILABLE customer THEN DO:
IF CURRENT-CHANGED customer THEN DO:
DISPLAY Customer.custnum customer.NAME WITH 1 COLUMN.
Assign customer.NAME.
END.
ELSE DO:
Assign 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.