6.13. Implementing Pessimistic Concurrency Without Using Database Locks
Problem
You need the safety of pessimistic locking without the overhead of database locks.
Solution
Use extra columns and stored procedures as shown in the following examples.
The schema of table TBL0613 used in this solution is shown in Table 6-19.
Table 6-19. TBL0613 schema
|
Column name |
Data type |
Length |
Allow nulls? |
|---|---|---|---|
|
Id |
|
4 |
No |
|
Field1 |
|
50 |
Yes |
|
Field2 |
|
50 |
Yes |
|
LockId |
|
16 |
Yes |
|
LockDateTime |
|
8 |
Yes |
The sample uses seven stored procedures, which are shown in Example 6-31 through Example 6-37:
SP0613_AcquireLockUsed to lock a record specified by an
Idparameter in the table TBL0613 in the database. The lock is effected by setting the LockId field of an unlocked record, where the value of theLockIdfield isnull, to a GUID specified by an input parameter.SP0613_ReleaseLockUsed to clear the lock on a record in the table TBL0613 by setting both the LockId and LockDateTime columns to
null. The record is identified by anIdparameter. ALockIdparameter—obtained by executing theSP0613_AcquireLockstored procedure—must be supplied to clear the lock on a record.SP0613_DeleteUsed to delete a record specified by an
Idparameter from the table TBL0613 in the database. ALockIdparameter—obtained by executing theSP0613_AcquireLockstored procedure—must be supplied to delete the record.SP0613_GetUsed to retrieve a record specified by an
Idparameter or all records ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access