Execution and Item Locks
Locks are flags set to disable certain normal functions. Two types of lock can be set from DataBasic.
-
An Execution Lock which enables exclusive access by a process to a user-defined resource, such as a file or process. It prevents execution by any process that uses the same execution lock until the lock is cleared.
Note
Execution locks can also be set from Proc using the PLn command.
-
An Item Lock which enables exclusive update access by a process to a file item. It prevents any attempts to lock and update the same item until the lock is released.
Note
Item locks are also set when editing an item using EDITOR or Screen Editor (except when in 'browse' mode), and can be set from Proc and ALL.
Execution Locks
In a DataBasic program an execution lock is set using the LOCK statement. Each execution lock is assigned a number from 0 to 255. So, for example, execution lock '54' is set by a program using the LOCK 54 statement. If that lock is not already set, then the LOCK statement sets it, then executes a THEN clause (if any). The THEN clause may execute a series of statements to access a file or item, or use some other user-defined resource. If the specified lock is already set by another process, then the LOCK statement will fail and will execute an ELSEclause., if specified. The program halts at the LOCK statement awaiting release if the lock is already set and no ELSE clause is specified.
The execution lock does not, of itself, prevent another process from executing. It only prevents another process from setting the same execution lock. In the example above, the second process is only allowed to set the execution lock and execute statements in the THEN clause (or those following the LOCK statement when lock 54 is cleared by the first process.
Note therefore that an execution lock is only effective when programs cooperate using the LOCK/UNLOCK statements and appropriate lock numbers.
Execution locks are cleared by one of the following:
-
Executing the UNLOCK statement.
-
Terminating the program.
Item Locks
Item locks can be used to prevent an item from being updated by more than one process at the same time. This maintains the integrity of the database while allowing multiple users access to other items in the same group.
DataBasic allows a reality process to lock the same item multiple times (this will normally occur if the item is processed by a number of different routines, each of which locks the item) - the item must be unlocked the same number of times (see Clearing Item Locks).
Item locks are associated with a file variable, assigned with the OPEN statement.
Note
If you copy the contents of a file variable to another variable any item locks are not replicated. The ReplicateItemLocks database configuration parameter allows you to change this behaviour.
The maximum number of items that can be locked on a database is user-defined.
Setting Item Locks
In a DataBasic program, an item lock is set by executing one of the following statements: READU, READVU, MATREADU.
Note
You cannot lock an item if it is locked by another process. READU, READVU or MATREADU all accept a LOCKED clause that is executed if the specified item is locked; If you do not supply a locked clause, the statement waits until the lock is released before continuing.
You can also set an item lock on a nonexistent item, so that a new item can be locked until it is written to disk. Such a lock can be cleared with the RELEASE statement if no write operation is carried out.
Clearing Item Locks
Item locks are cleared under the following circumstances:
-
If you write to the locked item with WRITE, WRITEV or MATWRITE (you can write to a locked item without unlocking it by using WRITEU, WRITEVU or MATWRITEU).
-
If you explicitly release a lock with the RELEASE statement.
-
When the file is closed, either with the CLOSE statement or automatically when the file variable goes out of scope or is assigned another value, this releases all locks held on items in that file. For example, if a file was opened to a local variable within an external subroutine, on return from the subroutine, the file will be closed and any item locks released. Similarly, when a program terminates, all files opened to local and local COMMON variables are automatically closed, releasing any locks held on these files.
Note
This behaviour can be changed with the RosStyleItemLocks database configuration parameter.
Locks held on files opened to variables in Named COMMON sections are not released until the user logs off, unless the file is explicitly closed with the CLOSE statement.
-
When the user logs off, any remaining locks held by that user are released.
Note
Within a transaction, release is deferred until the transaction is completed or aborted.