dbms notes
dbms notes
Concurrency control is used to address such conflicts which mostly occur with
a multi-user system. It helps you to make sure that database transactions are
performed concurrently without violating the data integrity of respective
databases.
Lost Updates occur when multiple transactions select the same row
and update the row based on the value selected
Uncommitted dependency issues occur when the second transaction
selects a row which is updated by another transaction (dirty read)
Non-Repeatable Read occurs when a second transaction is trying to
access the same row several times and reads different data each time.
Incorrect Summary issue occurs when one transaction takes
summary over the value of all the instances of a repeated data-item,
and second transaction update few instances of that specific data-item.
In that situation, the resulting summary does not reflect a correct result.
Lock-Based Protocols
Two Phase
Timestamp-Based Protocols
Validation-Based Protocols
Lock-based Protocols
A lock is a data variable which is associated with a data item. This lock
signifies that operations that can be performed on the data item. Locks help
synchronize access to the database items by concurrent transactions.
Binary Locks: A Binary lock on a data item can either locked or unlocked
states.
A shared lock is also called a Read-only lock. With the shared lock, the data
item can be shared between transactions. This is because you will never have
permission to update data on the data item.
For example, consider a case where two transactions are reading the account
balance of a person. The database will let them read by placing a shared lock.
However, if another transaction wants to update that account's balance,
shared lock prevent it until the reading process is over.
2. Exclusive Lock (X):
With the Exclusive Lock, a data item can be read as well as written. This is
exclusive and can't be held concurrently on the same data item. X-lock is
requested using lock-x instruction. Transactions may unlock the data item
after finishing the 'write' operation.
4. Pre-claiming Locking
Starvation
Deadlock
This locking protocol divides the execution phase of a transaction into three
different parts.
Growing Phase: In this phase transaction may obtain locks but may not
release any locks.
Shrinking Phase: In this phase, a transaction may release locks but not
obtain any new lock
It is true that the 2PL protocol offers serializability. However, it does not
ensure that deadlocks do not happen.
In the above-given diagram, you can see that local and global deadlock
detectors are searching for deadlocks and solve them with resuming
transactions to their initial states.
Strict Two-Phase Locking Method
Strict-Two phase locking system is almost similar to 2PL. The only difference
is that Strict-2PL never releases a lock after using it. It holds all the locks until
the commit point and releases all the locks at one go when the process is
over.
Centralized 2PL
In Centralized 2 PL, a single site is responsible for lock management process.
It has only one lock manager for the entire DBMS.
Distributed 2PL
In this kind of two-phase locking mechanism, Lock managers are distributed
to all sites. They are responsible for managing locks for data at that site. If no
data is replicated, it is equivalent to primary copy 2PL. Communication costs
of Distributed 2PL are quite higher than primary copy 2PL
Timestamp-based Protocols
The timestamp-based algorithm uses a timestamp to serialize the execution of
concurrent transactions. This protocol ensures that every conflicting read and
write operations are executed in timestamp order. The protocol uses
the System Time or Logical Count as a Timestamp.
The older transaction is always given priority in this method. It uses system
time to determine the time stamp of the transaction. This is the most
commonly used concurrency protocol.
Lock-based protocols help you to manage the order between the conflicting
transactions when they will execute. Timestamp-based protocols manage
conflicts as soon as an operation is created.
Example:
Advantages:
Disadvantages:
The wait for a graph for the above scenario is shown below:
Deadlock Recovery
A traditional operating system such as Windows doesn’t deal with deadlock recovery as
it is time and space consuming process. Real-time operating systems use Deadlock
recovery.
Recovery method
1. Killing the process: killing all the process involved in the deadlock. Killing process
one by one. After killing each process check for deadlock again keep repeating the
process till system recover from deadlock.
2. Resource Preemption: Resources are preempted from the processes involved in
the deadlock, preempted resources are allocated to other processes so that there
is a possibility of recovering the system from deadlock. In this case, the system
goes into starvation.