TLocks
Source: source/classes/tlocks.prg
Standalone class
TLocks provides a record-locking mechanism for ODBC tables. It manages a lock table within the database that tracks which records are locked and by which connection. This enables basic pessimistic concurrency control for ODBC data sources that do not provide native record-level locking.
Key DATA Members
| DATA | Type | Description |
|---|---|---|
oOdbc | Object | TODBC connection object for database access |
cTable | Character | Name of the table being locked |
nId | Numeric | Connection or session identifier |
Methods
| Method | Description |
|---|---|
New( oOdbc, cTable ) | Create a new lock manager for the specified ODBC connection and table |
CreateEnviroment() | Create the lock tracking environment (lock table) if it does not exist |
Lock( nRecNo ) | Lock a pseudo-record by record number |
UnLock( nRecNo ) | Release a lock on a pseudo-record |
RefreshLock( nRecNo ) | Refresh the lock timestamp to prevent timeout expiration |
SeekRegLock( nRecNo ) | Check if a pseudo-record is currently locked |
Example: Lock/Unlock a Pseudo-Record
#include "FiveWin.ch"
function Main()
local oOdbc, oLocks
oOdbc := TODBC():New( "MyDSN", "user", "pass" )
if oOdbc:lSuccess
oLocks := TLocks():New( oOdbc, "CUSTOMERS" )
// Ensure the lock environment exists
oLocks:CreateEnviroment()
// Lock record number 101
if oLocks:Lock( 101 )
MsgInfo( "Record 101 locked" )
// Do work with the record...
// Release the lock
oLocks:UnLock( 101 )
MsgInfo( "Record 101 unlocked" )
else
MsgAlert( "Could not lock record 101" )
endif
endif
return nil
Notes
- TLocks uses a separate lock table in the ODBC database to track record-level locks. This table is created automatically by
CreateEnviroment()and stores key information including record number, session ID, and lock timestamp. - Pessimistic locking means a lock is acquired before editing and held until explicitly released. Other sessions attempting to lock the same record will fail until the lock is released.
- Use
RefreshLock()periodically during long editing sessions to prevent the lock from timing out. SeekRegLock()allows checking whether a specific record is currently locked without attempting to acquire the lock.- Always release locks with
UnLock()as soon as the edit is complete to avoid blocking other users.