Skip to main content

Lock() and Unlock() Functions

generated description: locktable

Let's talk about how you use Lock() and Unlock(), and how they work. When you lock something, the system adds the argument of Lock() to the Lock Table. If another process tries to lock the same record, that process will wait until the first process releases the lock. The second process may select a different record for editing, and Lock() that record; the system will add that record to the Lock Table also. Lock() returns True if the operation succeeds, False otherwise. The second argument of Lock() is a timeout in seconds. If the operation doesn't succeed within the specified time, Lock() returns False.

Before running the example below, use the Management Portal, click System Explorer > Locks > View Locks and look at the Lock Table. When you run the example, it will wait several seconds before completing so you can see the lock. Keep clicking the Lock Table folder to refresh the display.

if lock(^x(1),3) then
    println "lock successful"
    ' wait several seconds so lock remains in Lock Table
    for i = 1 to 100000000 : next i 
    println "lock failed"
end if

To see a lock attempt fail, use the Terminal, and the ObjectScript Lock command, to add the same item to the Lock Table. Then run the example above again.

SAMPLES>lock ^x(1) ; add ^x(1) to the Lock Table before running example

SAMPLES>lock       ; to remove ^x(1) from the Lock Table afterwards

In the next version of your myBASlookup routine, you will write some code that allows the user to edit a person. This means you will be updating the ^PersonD and ^PersonI globals. In this case, once the user selects a record for editing, with a specific id, you will add this to your routine: lock(^PersonD(id)). After the new data is stored, you will unlock the record using unlock(^PersonD(id)).

FeedbackOpens in a new tab