Lock

See Also: Transactions and DDOs, Begin_Transaction, Close, DF_File_Mode, Open, Unload_Driver

Purpose

To prevent other users from making changes to a database while one user is saving a record.

Syntax

Lock

What It Does

Lock prevents any change (save or delete) from being made by any other user in a multi user system to all of a program's open database tables. It does not affect tables whose file-mode attribute has been set to either Alias or Read_Only.

Use the Lock command before issuing any Save or SaveRecord command. Lock does not interfere with other users' ability to read or process data—only changes to the database (saves) are prohibited. If a user tries to save while another has a lock in effect, the second user's program will wait until the lock by the first user is cleared or until a lock timeout occurs.

Lock remains in force until an Unlock command is executed. Therefore, Lock commands must be followed immediately by the Save or Saverecord command(s), and these must be followed immediately by an Unlock command to prevent the database from being locked for any appreciable length of time.

 

Lock should be used when saving new records.

Example

This example shows how to use lock when creating only a new record.

Clear Customer

Lock

    Move 101 to Customer.Id

    Move "Smith Brothers" to Customer.Name

    Saverecord Customer

Unlock

 

If any existing records are saved, or saved at the same time as a new record, the Reread command should be used instead of Lock. Reread does a lock and updates all active record buffers from the disk.

Example

This example shows how to use reread to reread an existing record from SysTable in order to use it to create a new customer record and get the next customer id to use (an autoincrement column) for the new customer record. Notice that only SysTable is passed on the reread line, because it is the only existing table in the transaction and thus requires rereading from disk prior to applying changes. Since reread, like lock, locks all open tables, both SysTable and Customer will be locked.

Clear Customer

Reread SysTable

    Increment SysTable.NextCustomerId
Move SysTable.NextCustomerId to Customer.Id
Move "Smith Brothers" to Customer.Name

    Saverecord Customer SysTable

Unlock

Leave lock in force only for the few commands for which it is needed, in order to minimize the time that other users are prevented from updating the database. Used properly, locks are so fast that they seldom interfere noticeably with the activities of other users.

On most systems, all tables a program has opened will be locked. Tables are locked in order of their numbers in the filelist.

Tables whose filemode is set to Alias or Read_Only are not affected by lock.

Locks are cumulative. You must issue an Unlock command for every Lock or Reread that executes. Each lock/reread increments a lock counter and each unlock decrements the lock counter until it reaches zero (0), at which time the lock is released. To determine how many locks are currently in effect, use the DF_TRAN_COUNT attribute.