- DBMS - Home
- DBMS - Overview
- DBMS - Architecture
- DBMS - Data Models
- DBMS - Data Schemas
- DBMS - Data Independence
- DBMS - System Environment
- Centralized and Client/Server Architecture
- DBMS - Classification
- Relational Model
- DBMS - Codd's Rules
- DBMS - Relational Data Model
- DBMS - Relational Model Constraints
- DBMS - Relational Database Schemas
- DBMS - Handling Constraint Violations
- Entity Relationship Model
- DBMS - ER Model Basic Concepts
- DBMS - ER Diagram Representation
- Relationship Types and Relationship Sets
- DBMS - Weak Entity Types
- DBMS - Generalization, Aggregation
- DBMS - Drawing an ER Diagram
- DBMS - Enhanced ER Model
- Subclass, Superclass and Inheritance in EER
- Specialization and Generalization in Extended ER Model
- Data Abstraction and Knowledge Representation
- Relational Algebra
- DBMS - Relational Algebra
- Unary Relational Operation
- Set Theory Operations
- DBMS - Database Joins
- DBMS - Division Operation
- DBMS - ER to Relational Model
- Examples of Query in Relational Algebra
- Relational Calculus
- Tuple Relational Calculus
- Domain Relational Calculus
- Relational Database Design
- DBMS - Functional Dependency
- DBMS - Inference Rules
- DBMS - Minimal Cover
- Equivalence of Functional Dependency
- Finding Attribute Closure and Candidate Keys
- Relational Database Design
- DBMS - Keys
- Super keys and candidate keys
- DBMS - Foreign Key
- Finding Candidate Keys
- Normalization in Database Designing
- Database Normalization
- First Normal Form
- Second Normal Form
- Third Normal Form
- Boyce Codd Normal Form
- Difference Between 4NF and 5NF
- Structured Query Language
- Types of Languages in SQL
- Querying in SQL
- CRUD Operations in SQL
- Aggregation Function in SQL
- Join and Subquery in SQL
- Views in SQL
- Trigger and Schema Modification
- Storage and File Structure
- DBMS - Storage System
- DBMS - File Structure
- DBMS - Secondary Storage Devices
- DBMS - Buffer and Disk Blocks
- DBMS - Placing File Records on Disk
- DBMS - Ordered and Unordered Records
- Indexing and Hashing
- DBMS - Indexing
- DBMS - Single-Level Ordered Indexing
- DBMS - Multi-level Indexing
- Dynamic B- Tree and B+ Tree
- DBMS - Hashing
- Query Processing and Optimization
- Heuristics in Query Processing
- Transaction and Concurrency
- DBMS - Transaction
- DBMS - Scheduling Transactions
- DBMS - Testing Serializability
- DBMS - Conflict Serializability
- DBMS - View Serializability
- DBMS - Concurrency Control
- DBMS - Lock Based Protocol
- DBMS - Timestamping based Protocol
- DBMS - Phantom Read Problem
- DBMS - Dirty Read Problem
- DBMS - Thomas Write Rule
- DBMS - Deadlock
- Backup and Recovery
- DBMS - Data Backup
- DBMS - Data Recovery
- DBMS Useful Resources
- DBMS - Quick Guide
- DBMS - Useful Resources
- DBMS - Discussion
DBMS - Lock-Based Protocol
In a DBMS, transactions are nothing but reading and writing operations that sometimes change the data from the tables. If there are multiple transactions working together, they must be concurrent to make the system run smoothly. Managing how computers share data is important when many tasks are taking place at the same time.
Without an effective controlling mechanism, data could get mixed up or lost. One common way to manage this is through a Lock-Based Protocol, which is a concurrency control protocol. It keeps things organized when multiple tasks need the same data.
Importance of Managing Data Access
Imagine there are several people working on the same document at the same time. Now if two people try to change the same sentence, their work could overlap or get erased. Databases face the same problem when many tasks run or execute at once. If we perfectly manage them the data stays accurate.
If we do not manage access, the problems like lost updates or reading unfinished work can happen. These problems are quite interesting. A lost update means one personâs changes erase anotherâs. Reading unfinished work means seeing something before it is ready. Proper data management can avoid these issues.
Databases in some large sectors like banks or online stores, there are thousands of tasks happen at the same time. Now without rules, the system could crash or give incorrect information. This is why data access management is a top priority in databases.
What is Lock-Based Protocol?
A lock-based protocol is to manage data use. When a task wants to access some data items, it locks it. This means no one else can change it until the task is done. It works like locking our vehicle outside a store. When it is locked, only we can use it.
Think of locking as a public restroom. when we enter into the room lock it from inside. No one can enter until we leave it. Similarly, when a task locks data in a database, other tasks have to wait.
Types of Locks in the System
There are two types of locks −
Shared Lock (S-Lock)
Shared locks are like borrowing a library book to read. Here multiple people can borrow different copies at once.
- Purpose − Lets a task read data but cannot change it.
- Behavior − Many tasks can share this lock at the same time.
- Example − If task T2 wants to read a file, then it uses a shared lock. After reading, it releases the lock.
Exclusive Lock (X-Lock)
An exclusive lock is like borrowing the only copy of a rare book. Only one person can have it at a time.
- Purpose − Lets a task read and change data.
- Behavior − Only one task can hold this lock at a time.
- Example − If task T1 wants to update a file, then it uses an exclusive lock. After updating, it releases the lock.
When Locks Work Together
Locks have rules about how they can be used together. Let us see how they work −
| Lock Type | Shared Lock | Exclusive Lock |
|---|---|---|
| Shared Lock | Allowed | Not Allowed |
| Exclusive Lock | Not Allowed | Not Allowed |
Example − If T1 uses a shared lock to read data, then the transaction T2 can also read it at the same time. But if T1 uses an exclusive lock, no one else can access the data until T1 finishes.
Shared locks work well when tasks only need to read data. But exclusive locks are needed when data changes or writing data are involved.
How Locking Works: An Example
Imagine two tasks −
- T1's Job − Change some data.
- T2's Job − Read the same data.
Let's see the steps in action.
T1 Locks the Data
- T1 locks the data with an exclusive lock.
- T1 reads and updates the data.
- T1 writes the new data.
- T1 unlocks the data.
T2 Reads the Data
- After T1 unlocks the data, T2 locks it with a shared lock.
- T2 reads the updated data.
- T2 unlocks the data.
Since T2 waits until T1 finishes, there is no confusion or data loss.
If T2 tried to read the data while T1 was still updating it, the system would block T2 to prevent problems.
Changing Lock Types: Upgrading and Downgrading
Sometimes, a task may need a stronger or weaker lock −
- Upgrading − Moving from a shared lock to an exclusive lock.
- Downgrading − Moving from an exclusive lock to a shared lock.
Example − If a task has a shared lock but then needs to update the data, it upgrades to an exclusive lock. After the update, if it only needs to read the data, it downgrades back to a shared lock.
Upgrading works is like borrowing a library book for reading but later needing it for an assignment that requires writing in the book (if allowed!). And downgrading would be finishing the notes and returning to just reading the book.
But upgrading only works when only one transaction is using the data in shared lock. Otherwise, we cannot upgrade it to exclusive mode.
Why Use Lock-Based Protocols?
Following are the benefits of using lock-based protocols −
- Keeps Data Correct − Makes sure data stays accurate.
- Allows Sharing − Lets tasks work together while staying organized.
- Stops Problems − Prevents errors like lost updates or reading unfinished work.
- Manages Resources − Makes the tasks use system resources properly.
Following are the challenges of using lock-based protocols −
- Getting Stuck − If two tasks wait for each other forever, it will be fall into a deadlock state.
- Being Left Behind − A task might wait too long if others keep taking priority (case of starvation).
- Extra Work − Managing locks can be complicated.
- System Slowdowns − Too many locks can slow the system.
Conclusion
In this chapter, we covered how lock-based protocols work in databases. We discussed why managing data is important, what locks are, and how they work. We also covered how locks can be changed and the advantages and disadvantages of using this method. There are other techniques for concurrency control, for example, timestamp-based protocols, which we will cover in the next chapter. These protocols have several advantages over lock-based method.