public final class LockingMechanism extends Object
Every table in the database has an 'access_queue' that is generated the first time the table is accessed. When a read or write request happens, the thread and the type of access is put onto the top of the queue. When the read/write access to the table has completed, the access is removed from the queue.
An access to the table may be 'blocked' until other threads have completed their access of the table.
A table that has a 'read lock' can not be altered until the table object is released. A table that has a 'write lock' may not be read until the table object is released.
The general rules are: a) A read request can go ahead if there are no write request infront of this request in the access queue. b) A write request can go ahead if the write request is at the front of the access queue.
This class requires some for-sight to which tables will be read/written to. We must pass all tables being read/written in a single stage. This implies a 2 stage process, the 1st determining which tables are being accessed and the 2nd performing the actual operations.
Some operations such as creating and dropping and modifying the security tables may require that no threads interfere with the database state while the operation is occuring. This is handled through an 'Excluside Mode'. When an object calls the locking mechanism to switch into exclusive mode, it blocks until all access to the database are complete, then continues, blocking all other threads until the exclusive mode is cancelled.
The locking system, in simple terms, ensures that any multiple read operations will happen concurrently, however write operations block until all operations are complete.
SYNCHRONIZATION: This method implements some important concurrent models for ensuring that queries can never be corrupted.
Modifier and Type | Field and Description |
---|---|
static int |
EXCLUSIVE_MODE |
static int |
SHARED_MODE
Class statics.
|
Constructor and Description |
---|
LockingMechanism(DebugLogger logger)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
finishMode(int mode)
This must be called when the calls to a Database object have finished.
|
boolean |
isInExclusiveMode()
Returns true if we are locked into exclusive mode.
|
LockHandle |
lockTables(DataTable[] t_write,
DataTable[] t_read)
This method locks the given tables for either reading or writing.
|
void |
reset()
Resets this object so it may be reused.
|
void |
setMode(int mode)
This method _must_ be called before a threads initial access to a Database
object.
|
void |
unlockTables(LockHandle handle)
Unlocks the tables that were previously locked by the 'lockTables' method.
|
public static final int SHARED_MODE
public static final int EXCLUSIVE_MODE
public LockingMechanism(DebugLogger logger)
public void reset()
public LockHandle lockTables(DataTable[] t_write, DataTable[] t_read)
NOTE: ** IMPORTANT ** We must ensure that a single Thread can not create multiple table locks. Otherwise it will cause situations where deadlock can result. NOTE: ** IMPORTANT ** We must ensure that once a lock has occured, it is unlocked at a later time _no matter what happens_. Otherwise there will be situations where deadlock can result. NOTE: A LockHandle should not be given to another Thread.
SYNCHRONIZATION: This method is synchronized to ensure multiple additions to the locking queues can happen without interference.
public void unlockTables(LockHandle handle)
public boolean isInExclusiveMode()
public void setMode(int mode)
public void finishMode(int mode)
Copyright © 2015. All rights reserved.