Product Documentation

c-treeDB API for C++ - Developers Guide

Previous Topic

Next Topic

Record Locking

A record lock can be acquired by performing the following actions:

  1. Read a record from disk by calling one of the CTRecord::First(), CTRecord::Last(), CTRecord::Next(), CTRecord::Prev(), CTRecord::SeekRecord(), CTRecord::Find(), or CTRecord::FindTarget() methods.
  2. Lock the record by calling CTRecord::LockRecord().

Session wide locks are better suited to implement data integrity because the records are locked as they are read from disk. Since a record lock can only be applied after the record is read, there is a window of opportunity for the record be modified or deleted by another thread before the record lock is acquired.

Release a record lock by calling CTRecord::UnlockRecord() or by calling CTRecord::LockRecord() with CTLOCK_FREE mode.

In This Section

Check if a record is locked

Demoting record locks

Previous Topic

Next Topic

Check if a record is locked

Call CTRecord::GetRecordLock() to retrieve the current lock mode acquired for the current record. If the record is not locked, CTRecord::GetRecordLock() returns CTLOCK_FREE. If the current record is locked, CTRecord::GetRecordLock() returns the record lock mode.

// check if record is locked

if (ARecord.GetRecordLock() != CTLOCK_FREE)

{

printf("The record is locked\n");

}

The following record modes are returned by ctdbGetRecordLock():

Lock mode

Explanation

CTLOCK_FREE

The record is not locked

CTLOCK_READ

The record has a read lock

CTLOCK_WRITE

The record has a write lock

Previous Topic

Next Topic

Demoting record locks

If a record has acquired a write lock, it is possible to change or demote the write lock to a read lock, if the record has not been updated. Use the CTRecord::LockRecord() method to demote a record with a write lock to a read lock.

// demote a write lock to a read lock

if (ARecord::GetRecordLock() == CTLOCK_WRITE)

{

try

{

ARecord.LockRecord(CTLOCK_READ);

}

catch (CTException &err)

{

printf("Demote lock failed with error %d\n", err.GetErrorCode());

}

}

TOCIndex