A record lock can be acquired by performing the following actions:
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.
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 |
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());
}
}