Execution and Item Locks

Locks are flags set to disable certain normal functions. Two types of lock can be set from DataBasic.

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:

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:

Note

Within a transaction, release is deferred until the transaction is completed or aborted.

See Also

Locks and the Locking Systems, Efficiency Guidelines.