Prevent Locks

26 Views


To prevent record locks or lock-related errors when using CHAIN, UPDATE, or DELETE in RPGLE, you can use a combination of safe access methods, error handling, and access strategies. Here’s how to do it effectively.


✅ 1. Use N Operation Extender (CHAIN(N)): No Lock Read

Use this if you only want to read the record without locking it.

Chain (N: CustomerID) Customers;

If %Found(Customers);
   Dsply 'Read without locking.';
EndIf;

🔸 Best for display-only or logging purposes.


✅ 2. Use INFSR or MONITOR for Error Handling

Use MONITOR / ON-ERROR blocks or %ERROR with %STATUS to catch locks safely.

Example:

Monitor;
   Chain (CustomerID) Customers;

   If %Found(Customers);
      Name = 'Safe Update';
      Update Customers;
   EndIf;

On-Error 1218;
   Dsply 'Record is locked, skipping update.';
EndMon;

✅ 3. Avoiding Locking Conflicts

A. Use CHAIN(N) followed by UPDATE only if needed:

Chain (N: CustomerID) Customers; // No lock here

If %Found(Customers);
   // Do something, then lock only when updating
   Chain (CustomerID) Customers; // With lock
   If Not %Error;
      Name = 'Final Update';
      Update Customers;
   ElseIf %Status = 1218;
      Dsply 'Locked during update.';
   EndIf;
EndIf;

This limits the time the record is locked.


✅ 4. Use Record-Level Lock Wait Option in F-spec

In your DCL-F file declaration:

Dcl-F Customers Usage(*Update) Keyed LockWait(0);
LockWait(x) Behavior
LockWait(0) Do not wait if record is locked
LockWait(5) Wait 5 seconds before giving up
LockWait(*NoWait) Synonym for 0-second wait
LockWait(*MaxWait) Wait indefinitely

If lock fails, use %ERROR and %STATUS to handle gracefully.


✅ 5. Avoid READ or CHAIN inside tight loops on busy files

If the file is heavily used, reduce lock time and avoid nested CHAIN/UPDATE inside long loops. Instead, load data in arrays or use embedded SQL cursors with WITH RS USE AND KEEP SHARE LOCKS.


✅ 6. Use *IN indicators for legacy-style lock checks

For older-style RPG:

C     Chain(E)  KeyField   MyFile   90
C     If *IN90
C       // Locked or not found
C     EndIf

 

Post Comments