RecLock and MsUnlock in introductory maintenance
Checks a lock before changing a record.
Overview
RecLock with .F. tries to lock the current record; .T. starts an insert. Assign fields only after .T., then finish with MsUnlock in the same alias. SQL cursors and read-only aliases cannot use this flow. Within an explicit transaction MsUnlock is still required, but locking can persist until transaction end. This example is only a no-assignment update cycle on an open positioned sandbox ZZ1, not a complete registration routine. Prefer supported automatic routines/models for standard tables. (Sources: refs. 1–3.)
Commented example
#Include "Protheus.ch"
User Function Ed613()
// Run only on an open, positioned sandbox ZZ1.
If Select("ZZ1") > 0
If !ZZ1->(Eof())
If RecLock("ZZ1",.F.)
// Validated assignments would be made here.
ZZ1->(MsUnlock())
EndIf
EndIf
EndIf
Return NilWhen preconditions hold, attempts to lock and finish without field changes. No writes on lock failure. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.
- SoftLock is not proof that RecLock succeeded. Plan errors/restoration without unlocking another owner’s resources.
- Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

Comments
There are no approved comments yet.
Sign in with Google or Microsoft to comment.