Accuracer: The First And Only BDE Alternative
Client-Server Single-File Embedded Database for Delphi / C++Builder / Kylix



Locking Mechanism

Previous  Top  Next


Introduction

Accuracer can be used both in multi-user and multi-thread environments.
Locking Mechanism is used for synchronization between differnent users or threads that modifies same database objects simultaneously. Locking protects data from reading or modification by other users or theads.

There are following locking objects in Accuracer:
- Low-Level database objects (Free Space Manager and Tables List) - used internally by Accuracer database engine.
- High-Level database objects (Tables and Records) - used by both Accuracer database engine and end-users.

Free Space Manager is a system that adds and removes pages (for later reuse) to the database file.
Table List is a system that handles tables stored inside the database file - allows to find, create, delete and rename tables.

Locking Mechanism (Locks Manager) implemented in accuracer performs all required locks automatically. There are two modes of the Locks Manager: InMemory and Disk modes.

InMemory mode is used when database file is opened Exclusively, that prevents it from modification by other applications.
Each session connected to the database file gets its unique identifier called SessionID. Different sessions are treated as different users, so modifications made by the transaction in one session cannot be viewed by other sessions.
Also each session before locking table or record checks this lock type for compatibility with locks set by other session.
Single user version of Accuracer always uses InMemory mode. Locks Manager in InMemory mode does not locks bytes in the database file and works much faster than Disk mode.

Disk mode is designed for multi-user access to the database file (by different applications). In this mode Locks Manager performs locking and unlocking of physical bytes in the database file. There are reserved pages for the database and for each table for locking. The space required for Locks Manager is 1 byte for each connection to the database file and 11 bytes for each connection to each table. So maximum number of connectons (TACRDatabase.Options.MaxSessionCount) influences both size of the database file and performance of multi-user access to this file. If you set greater number of maximum concurrent connections each lock operation will work slower and vice versa. This approach allows to get high performance for locks checking and use Accuracer engine under different platforms. Most of all modern operating systems allows to lock and unlock bytes inside the database file, while other techniques may have problems under different platforms. For example, locking of bytes beyound the file works in all versions of MS Windows, but never work under Unix, Linux and Novell.

Lock modes

There are following lock types in Accuracer:
- IS - used for opening a table in shared mode, remains until close
- X - used for opening a table in exclusive mode, remains until close
- S - used for shortly locking the table in Read Only mode
- IRW - used for continious locking the table in ReadOnly mode
- RW - used for shortly locking the table in Exclusvie mode
- U - used for continious or shortly locking records (Edit, Delete operation in TACRDataset)
Each Session can lock only single record in the table. If one session locks record (U mode), other sessions cannot lock this record and cannot edit or update it, however they can read this record.

Before locking table or record Locks Manager checks new lock type for compatibility with locks set by other sessions.
If lock fails this session waits a timeout sepcefied in TACRDatabase.LockParams.Delay and tries to set lock again.
If number of unsuccessfully retries exceeds the value specified in TACRDatabase.LockParams.RetryCount an exception will be raised.

Table locks comaptibility schema:

Lock Mode

X

IS

S

IRW

RW

X

NO

NO

NO

NO

NO

IS

NO

YES

YES

YES

YES

S

NO

YES

YES

YES

NO

IRW

NO

YES

YES

NO

NO

RW

NO

YES

NO

NO

NO




Performance Optimization

There are following methods of improving multi-user access performance:
1) Use transactions
2) Open tables in Exclusive mode for critical operations
3) Combine methods 1 and 2

If you do not need multi-user access you can also open the whole database in Exclusive mode.


© AidAim Software Accuracer: Contents-cross-platform Windows Linux Databas