3. Locking
Every programmer who has developed database programs understands the
potential for concurrency problems.
When one program tries to read data that is in the process of being changed by
another program, the DBMS must prohibit access until the modification is
complete, in order to ensure data integrity.
The DBMS uses a locking mechanism to enable multiple, concurrent users to
access and modify data in the database.
Locks are used to ensure the integrity of data. When a database resource is
locked by one process, another process is not permitted to change the locked
data.
4. Types of Locks
At a very basic level, a DBMS will take a write lock when it writes information or
a read lock when it reads information.
A write occurs for INSERT, UPDATE, and DELETE statements. A read occurs for
SELECT statements.
But to actually accomplish such locking, the typical DBMS will use two basic
types of locks: shared locks and exclusive locks
5. Types of Locks
1. Shared Lock
Shared lock is also called read lock, used for reading data items only.
Shared locks support read integrity. They ensure that a record is not in process of being
updated during a read-only request.
Shared locks can also be used to prevent any kind of updates of record.
It is denoted by Lock-S.
For example, consider a case where initially A=100 and there are two transactions which are
reading A. If one of transaction wants to update A, in that case other transaction would be
reading wrong value. However, Shared lock prevents it from updating until it has finished reading.
6. Types of Locks
2. Exclusive Lock
With the Exclusive Lock, a data item can be read as well as written. Also called write lock.
They can be owned by only one transaction at a time.
It is denoted as Lock-X.
For example, consider a case where initially A=100 when a transaction needs to deduct 50 from A.
We can allow this transaction by placing X lock on it. Therefore, when the any other transaction
wants to read or write, exclusive lock prevent it.