Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Aptitude
  • Engineering Mathematics
  • Discrete Mathematics
  • Operating System
  • DBMS
  • Computer Networks
  • Digital Logic and Design
  • C Programming
  • Data Structures
  • Algorithms
  • Theory of Computation
  • Compiler Design
  • Computer Org and Architecture
Open In App
Next Article:
Introduction to TimeStamp and Deadlock Prevention Schemes in DBMS
Next article icon

Introduction to TimeStamp and Deadlock Prevention Schemes in DBMS

Last Updated : 05 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Database Management Systems (DBMS), deadlocks occur when two or more transactions are waiting for each other to release a resource, leading to an indefinite wait. Deadlocks are a common issue in concurrency control, especially when multiple transactions try to access the same data. To avoid this problem, different techniques like Conservative Two-Phase Locking (2PL) and Graph-Based Protocols are used, but they have some limitations.

A more effective approach is Timestamp-Based Deadlock Prevention. A timestamp (TS) is a unique identifier assigned to each transaction by the DBMS. It helps determine the execution order of transactions and ensures that older transactions are given priority, reducing the chances of deadlocks. Each transaction is assigned a unique timestamp based on the system clock.

  • When a transaction requests access to a data item, the system compares the timestamp of the transaction with the last transaction that accessed that data.
  • If the requesting transaction's timestamp is older than the last transaction's timestamp, the system rolls back the requesting transaction. This ensures that the transactions execute in a way that preserves their serializability (meaning the result is the same as if the transactions were executed one by one).

There are different ways to generate timestamps:

  1. Using a Counter – A simple number that increases for every new transaction.
  2. Using System Clock – Assigning the transaction a timestamp based on the current date and time.

Timestamp-based scheduling helps in maintaining serializability, ensuring transactions execute in a consistent order and avoiding conflicts. It is widely used in modern DBMS for better transaction management and deadlock prevention.

Deadlock Prevention Schemes based on Timestamps

To prevent any deadlock situation in the system, the DBMS aggressively inspects all the operations, where transactions are about to execute. The DBMS inspects the operations and analyzes if they can create a deadlock situation. If it finds that a deadlock situation might occur, then that transaction is never allowed to be executed. There are deadlock prevention schemes that use timestamp ordering mechanism of transactions in order to predetermine a deadlock situation.

Deadlock-Prevention-Schemes-based-on-Timestamp_
  • Wait_Die : An older transaction is allowed to wait for a younger transaction, whereas a younger transaction requesting an item held by an older transaction is aborted and restarted. 
    From the context above, if TS(Ti) < TS(Tj), then (Ti older than Tj) Ti is allowed to wait; otherwise abort Ti (Ti younger than Tj) and restart it later with the same timestamp.
Deadlock-Prevention-Schemes-based-on-Timestamp-2
  • Wound_Wait: It is just the opposite of the Wait_Die technique. Here, a younger transaction is allowed to wait for an older one, whereas if an older transaction requests an item held by the younger transaction, we preempt the younger transaction by aborting it. 
    From the context above, if TS(Ti) < TS(Tj), then (Ti older than Tj) Tj is aborted (i.e., Ti wounds Tj) and restarts it later with the same Timestamp; otherwise (Ti younger than Tj) Ti is allowed to wait.
Deadlock-Prevention-Schemes-based-on-Timestamp-3

Thus, both schemes end up aborting the younger of the two transactions that may be involved in a deadlock. It is done on the basis of the assumption that aborting the younger transaction will waste less processing which is logical. In such a case there cannot be a cycle since we are waiting linearly in both cases. 

Deadlock Prevention Without Timestamps

Some deadlock prevention techniques do not require timestamps. Two such methods are:

1. No-Wait Algorithm

  • If a transaction cannot get a lock, it is immediately aborted and restarted after some time.
  • This method completely eliminates waiting, so deadlocks cannot occur.
  • However, it is not practical, as transactions may restart too often, leading to unnecessary delays.

2. Cautious Waiting

  • When a transaction Ti tries to lock a resource X, but X is already locked by another transaction Tj, one of two things happens:
    1. If Tj is not waiting for any other resource, Ti is allowed to wait.
    2. If Tj is already waiting for another resource, Ti is aborted to prevent a possible deadlock.

This method reduces unnecessary aborts compared to the No-Wait Algorithm while still preventing deadlocks.

Deadlock Detection Using Wait-for Graph

In a database, when a transaction keeps waiting forever to get a lock, the DBMS should check if the transaction is involved in a deadlock. To do this, the lock manager uses a Wait-for Graph to detect deadlocks.

Wait-for Graph

  • This graph shows transactions and the locks they are waiting for.
  • If the graph has a cycle (a loop where transactions are waiting on each other), it means there is a deadlock.

In simple terms, if the transactions are stuck in a cycle, the system knows that a deadlock has occurred and can take action to fix it.

Starvation in Databases

Starvation is a problem that can happen when locking is used in databases. It occurs when a transaction is unable to proceed for a long time while other transactions are able to continue normally. This happens when the system unfairly gives priority to some transactions over others, causing certain transactions to keep waiting indefinitely.

Solution to Starvation

One solution to avoid starvation is to use a First Come, First Serve (FCFS) method. This means that transactions are allowed to lock items in the order they requested the lock. This ensures that no transaction waits forever while others proceed.

Role of the Concurrency Control Manager

The Concurrency Control Manager is responsible for scheduling transactions. It uses different methods, like the FCFS queue, to make sure starvation is avoided and transactions are processed fairly. This system helps manage transactions effectively to prevent delays or unfair blocking of certain transactions.

Try this question: GATE | GATE-CS-2017 (Set 1) | Question 46 


Next Article
Introduction to TimeStamp and Deadlock Prevention Schemes in DBMS

Z

zerocool
Improve
Article Tags :
  • Misc
  • DBMS
  • GATE CS
  • Deadlocks
Practice Tags :
  • Misc

Similar Reads

    Database Recovery Techniques in DBMS
    Database Systems like any other computer system, are subject to failures but the data stored in them must be available as and when required. When a database fails it must possess the facilities for fast recovery. It must also have atomicity i.e. either transactions are completed successfully and com
    11 min read
    ACID Properties in DBMS
    In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
    8 min read
    Why recovery is needed in DBMS
    Basically, whenever a transaction is submitted to a DBMS for execution, the operating system is responsible for making sure or to be confirmed that all the operations which need to be performed in the transaction have been completed successfully and their effect is either recorded in the database or
    6 min read
    Types of Schedules in DBMS
    Schedule, as the name suggests, is a process of lining the transactions and executing them one by one. When there are multiple transactions that are running in a concurrent manner and the order of operation is needed to be set so that the operations do not overlap each other, Scheduling is brought i
    7 min read
    Conflict Serializability in DBMS
    A schedule is a sequence in which operations (read, write, commit, abort) from multiple transactions are executed in a database. Serial or one by one execution of schedules has less resource utilization and low throughput. To improve it, two or more transactions are run concurrently. Conflict Serial
    5 min read
    Precedence Graph for Testing Conflict Serializability in DBMS
    A Precedence Graph or Serialization Graph is used commonly to test the Conflict Serializability of a schedule. It is a directed Graph (V, E) consisting of a set of nodes V = {T1, T2, T3..........Tn} and a set of directed edges E = {e1, e2, e3..................em}. The graph contains one node for eac
    6 min read
    Recoverability in DBMS
    Recoverability is a critical feature of database systems. It ensures that after a failure, the database returns to a consistent state by permanently saving committed transactions and rolling back uncommitted ones. It relies on transaction logs to undo or redo changes as needed. This is crucial in mu
    6 min read
    Deadlock in DBMS
    In a Database Management System (DBMS), a deadlock occurs when two or more transactions are waiting indefinitely for one another to release resources (such as locks on tables, rows, or other database objects). This results in a situation where none of the transactions can proceed, effectively bringi
    8 min read
    Starvation in DBMS
    Starvation in DBMS is a problem that happens when some processes are unable to get the resources they need because other processes keep getting priority. This can happen in situations like locking or scheduling, where some processes keep getting the resources first, leaving others waiting indefinite
    8 min read
    Lock Based Concurrency Control Protocol in DBMS
    In a DBMS, lock-based concurrency control is a method used to manage how multiple transactions access the same data. This protocol ensures data consistency and integrity when multiple users interact with the database simultaneously.This method uses locks to manage access to data, ensuring transactio
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences