CH 4
CH 4
Concurrency control in databases is essential for ensuring that multiple transactions can be
executed simultaneously without leading to data inconsistency. It addresses the challenges that
arise when transactions interact with shared data, aiming to maintain the integrity and
consistency of the database. Here are the key concepts and techniques involved in concurrency
control:
Integrity and consistency are fundamental concepts in database management that ensure data
remains accurate, reliable, and valid throughout its lifecycle. Here’s a detailed explanation of
each concept along with examples to illustrate their importance.
Data Integrity
Data integrity refers to the accuracy and consistency of data stored in a database. It ensures that
the data is correct, reliable, and maintained according to defined rules and constraints. There are
several types of data integrity:
Data Consistency
Data consistency refers to the state of data where all copies or instances are the same across all
systems and databases. It ensures that data remains accurate and coherent across different
database systems, applications, and platforms. Here are some examples of data consistency:
Locking techniques are fundamental methods used in concurrency control to ensure that multiple
transactions can operate safely in a shared database environment. Here are the main types of
locking techniques:
Shared Locks: Allow multiple transactions to read a resource concurrently but prevent any
transaction from writing to it. Useful for read-heavy operations. Allows a single transaction to
1
read and modify a resource. Prevents other transactions from accessing the resource until the
lock is released.
Allow multiple transactions to read a resource but prevent any modifications.
Example: A transaction viewing a bank account balance must acquire a shared lock.
Exclusive Locks: Allows a single transaction to read and modify a resource. Prevents other
transactions from accessing the resource until the lock is released.
Allow only one transaction to write to a resource, preventing others from reading or
writing while the lock is held.
Ensures data integrity during modifications.
Example: A transaction updating a bank account balance must acquire an exclusive lock.
Transaction A acquires a shared lock to read a record.
Transaction B also acquires a shared lock on the same record to read it.
Transaction C tries to acquire an exclusive lock to modify the record. It must wait until
both A and B release their shared locks.
2. Two-Phase Locking (2PL)
This technique involves two phases: the Growing Phase, where a transaction can acquire locks
but cannot release them, and the Shrinking Phase, where it can release locks but cannot acquire
new ones. For example, if Transaction T1 locks a record to update it, it must complete its updates
before releasing the lock, preventing other transactions from accessing the same record until T1
is finished. This helps avoid conflicts and ensures that transactions are executed in a controlled
manner.
2
Growing Phase: Locks are acquired but not released.
Shrinking Phase: Locks are released and no new locks can be acquired.
In database management systems, a locking point refers to a specific moment during a
transaction when a lock is acquired on a data item to control access and ensure data
integrity. Locking points are crucial for managing concurrency, especially in
environments where multiple transactions may attempt to read or write the same data
simultaneously.
Advantages: Guarantees serializability.
Disadvantages: Can lead to deadlocks and requires deadlock detection or prevention
mechanisms.
3. Timestamp Ordering
In this method, each transaction is assigned a unique timestamp. The system uses these
timestamps to determine the order of operations. For instance, if Transaction T1 has an earlier
timestamp than T2, T1's operations will be prioritized. If T1 reads a data item, and T2 attempts
to write to it, T2 will be delayed until T1 completes, ensuring that the database remains
consistent
This optimistic approach allows transactions to execute without restrictions until they reach a
validation phase. For example, a transaction may read and modify data freely, but before
committing, it checks whether any other transactions have modified the data it read. If conflicts
are detected, the transaction is rolled back. This method is effective in environments where
conflicts are rare.
Description: Transactions proceed without locking resources, but check for conflicts
before committing.
3
Usage: Suitable for environments with low contention.
Advantages: Reduces lock overhead and increases concurrency.
Disadvantages: May lead to higher abort rates under contention.
5. Multi-Version Concurrency Control (MVCC)
MVCC allows multiple versions of a data item to exist simultaneously. When a transaction reads
a data item, it accesses the version that was current at the time the transaction started. For
example, if Transaction T1 updates a record, T2 can still read the old version of that record
without being blocked. This increases concurrency and reduces waiting times for read operations
Description: Allows multiple versions of a data item, enabling readers to access the most
recent version without blocking writers.
Advantages: High concurrency and no read locks, suitable for read-heavy applications.
Disadvantages: Increased storage requirements and complexity in managing versions.
Choosing the appropriate locking technique depends on the specific application
requirements, transaction patterns, and workload characteristics. Balancing concurrency,
performance, and data integrity is key to effective concurrency control in database
systems.
4
• Example: During a major upgrade or maintenance task, a database administrator might lock the
entire database to prevent any access while backups are taken or schema changes are applied.
Performing a migration or applying a large batch update that affects all tables.
2. Table-Level Granularity
Locks an entire table.
• Example: If a company needs to perform a bulk insert of records into the Employees table, it
may lock the entire table to ensure that no other transactions can read or write to it during the
operation.
Bulk data imports or exports where consistency across the entire table is required.
3. Page-Level Granularity
Locks a specific page (a fixed-size block of data).
• Example: In a database where each page contains 100 rows, if a transaction updates rows 1-50,
it might lock the page containing those rows instead of locking each row individually. This
allows other transactions to access other pages concurrently.
When many users are accessing different parts of a large table, and some operations require
locking several rows that fit within a single page.
4. Row-Level Granularity
Locks individual rows within a table.
• Example: If two transactions are updating different rows in the Orders table (e.g., one updating
Order ID 101 and another updating Order ID 102), they can proceed simultaneously without
interfering with each other due to row-level locking.
High-concurrency environments like online transaction processing (OLTP) systems where
multiple users frequently update different records.
5. Field-Level Granularity
Locks specific fields (columns) within a row.
In an Account table, if one transaction is updating the balance field while another transaction is
updating the status field of the same row, field-level locking allows both operations to occur
simultaneously without conflict.
Situations where only certain fields in a record need to be updated frequently, such as in
financial applications where different attributes of an account can be modified independently.
Multiple Granularity Locking (MGL)
5
MGL organizes the database into a tree structure where each node represents a different level of
granularity. When a transaction locks a node, it implicitly locks all its descendant nodes in the
same mode. For example, if a transaction locks a file in exclusive mode, it automatically locks
all records within that file in exclusive mode as well.
Lock Modes in MGL
Multiple Granularity Locking is a locking mechanism that allows for different levels of locking
granularity within a database. It provides a hierarchy of locks that can be applied at different
levels, enabling more efficient concurrency control.
1. Locking Hierarchy:
• Locks can be applied at various levels, such as database, table, page, and row levels.
• A lock at a higher level (e.g., table) implicitly includes locks at all lower levels (e.g., all rows
within that table).
2. Lock Modes:
• Shared Lock (S): Allows multiple transactions to read data but not modify it.
• Exclusive Lock (X): Allows a transaction to read and modify data, preventing other
transactions from accessing it.
• Intention Locks: Used to indicate that a transaction intends to acquire locks at a lower level
(e.g., intention shared (IS) or intention exclusive (IX)).
3. Lock Compatibility:
• Different lock modes have different compatibility rules. For example, two transactions can
hold shared locks on the same resource simultaneously, but an exclusive lock is incompatible
with any other lock.
4. Deadlock Prevention:
• MGL helps in managing deadlocks by providing a structured way of acquiring locks.
Transactions must acquire intention locks before acquiring finer-grained locks, which helps to
avoid circular wait conditions.
Advantages of Multiple Granularity Locking
• Increased Concurrency: By allowing finer-grained locks, MGL enables more transactions to
operate simultaneously without interfering with each other.
• Reduced Overhead: It reduces the overhead associated with managing many individual locks
by allowing higher-level locks that cover multiple lower-level resources.
6
• Flexibility: Transactions can choose the appropriate level of locking based on their needs,
balancing between performance and data integrity.
Understanding the granularity of data items and implementing Multiple Granularity Locking are
crucial for optimizing database performance in multi-user environments. MGL provides a
structured approach to locking that enhances concurrency while maintaining data integrity,
making it a valuable technique in database management systems.
Example
Consider a banking database where transactions might involve multiple accounts and their
balances. Here’s how Multiple Granularity Locking might work:
Transaction A
Goal: Transfer money from Account A to Account B.
Steps:
Acquire an Exclusive Lock on Table (to prevent other transactions from accessing any
account during the transfer).
Acquire an Exclusive Lock on Row (R1) (Account A) to read and modify the balance.
Acquire an Exclusive Lock on Row (R2) (Account B) to read and modify the balance.
Transaction B
Goal: Check balances of Account C and Account D.
Steps:
Acquire a Shared Lock on Table (to read any account balance).
Acquire a Shared Lock on Row (R3) (Account C).
Acquire a Shared Lock on Row (R4) (Account D).
1.3. Using Locks for Concurrency Control in Indexes
Using locks for concurrency control in database indexes is essential for maintaining data
integrity and ensuring that multiple transactions can operate simultaneously without interfering
with each other. Here’s an overview of how locks function in this context:
Types of Locks
7
1. Shared Locks: These locks allow multiple transactions to read data concurrently. When
a transaction holds a shared lock on an index, other transactions can also acquire shared
locks but cannot modify the data until the shared lock is released.
2. Exclusive Locks: These locks are used when a transaction needs to modify data. An
exclusive lock prevents other transactions from acquiring either shared or exclusive locks
on the same index until the lock is released. This ensures that no other transaction can
read or write to the data being modified, thus maintaining data consistency.
Index Locking: This technique is crucial for maintaining the integrity of indexes during
transactions. When a transaction accesses a portion of an index, that portion is locked to
prevent other transactions from modifying or reading it simultaneously. This is
particularly important during operations like inserts, updates, or deletes, which require
the index to be updated accordingly.
Two-Phase Locking Protocol: This protocol is often employed to ensure that
transactions acquire locks in a way that prevents deadlocks and ensures serializability. It
requires that all locks be acquired before any are released, which helps in maintaining a
consistent state of the index during concurrent operations.
Specialized Techniques
Different index structures may require specialized locking techniques. For example, B-trees,
which are commonly used as database indexes, have specific concurrency control methods that
are more efficient than general locking mechanisms. These techniques allow for finer control
over how locks are applied, reducing contention and improving performance.