Dirty Read Problem

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Dirty read problem (W-R conflict) uncommitted dependency

problem

This type of problem occurs when one transaction T1 updates a data item of the database,
and then that transaction fails due to some reason, but its updates are accessed by some
other transaction.
Example: Let’s take the value of A is 100

Time Transaction T1 Transaction T2

t1 Read(A)  

t2 A=A+20  

t3 Write(A)                  

t4   Read(A)

t5   A=A+30

t6   Write(A)

t7 Write(B)  

Here,
 At t1 time, T1 transaction reads the value of A i.e., 100.
 At t2 time, T1 transaction adds the value of A by 20.
 At t3 time, T1transaction writes the value of A (120) in the database.
 At t4 time, T2 transactions read the value of A data item i.e., 120.
 At t5 time, T2 transaction adds the value of A data item by 30.
 At t6 time, T2transaction writes the value of A (150) in the database.
 At t7 time, a T1 transaction fails due to power failure then it is rollback according to
atomicity property of transaction (either all or none).
 So, transaction T2 at t4 time contains a value which has not been committed in the
database. The value read by the transaction T2 is known as a dirty read.
Unrepeatable read (R-W Conflict) or the inconsistency
analysis problem

It is also known as an inconsistent retrieval problem. If a transaction T 1 reads a value of data


item twice and the data item is changed by another transaction T 2 in between the two read
operation. Hence T1 access two different values for its two read operation of the same data
item.
Example: Let’s take the value of A is 100

Time Transaction T1 Transaction T2

t1 Read(A)  

t2   Read(A)

t3   A=A+30

t4   Write(A)

t5 Read(A)  

Here,
 At t1 time, T1 transaction reads the value of A i.e., 100.
 At t2 time, T2transaction reads the value of A i.e., 100.
 At t3 time, T2 transaction adds the value of A data item by 30.
 At t4 time, T2 transaction writes the value of A (130) in the database.
 Transaction T2 updates the value of A. Thus, when another read statement is
performed by transaction T1, it accesses the new value of A, which was updated by T2.
Such type of conflict is known as R-W conflict. 
Lock-Based Protocol
In this type of protocol, any transaction cannot read or write data until it acquires an
appropriate lock on it. There are two types of lock:

1. Shared lock:

o It is also known as a Read-only lock. In a shared lock, the data item can only read by the
transaction.
o It can be shared between the transactions because when the transaction holds a lock,
then it can't update the data on the data item.

2. Exclusive lock:

o In the exclusive lock, the data item can be both reads as well as written by the
transaction.
o This lock is exclusive, and in this lock, multiple transactions do not modify the same data
simultaneously.

There are four types of lock protocols available:


1. Simplistic lock protocol
It is the simplest way of locking the data while transaction. Simplistic lock-based
protocols allow all the transactions to get the lock on the data before insert or delete or
update on it. It will unlock the data item after completing the transaction.

2. Pre-claiming Lock Protocol

o Pre-claiming Lock Protocols evaluate the transaction to list all the data items on which
they need locks.
o Before initiating an execution of the transaction, it requests DBMS for all the lock on all
those data items.
o If all the locks are granted then this protocol allows the transaction to begin. When the
transaction is completed then it releases all the lock.
o If all the locks are not granted then this protocol allows the transaction to rolls back and
waits until all the locks are granted.

3. Two-phase locking (2PL)

o The two-phase locking protocol divides the execution phase of the transaction into three
parts.
o In the first part, when the execution of the transaction starts, it seeks permission for the
lock it requires.
o In the second part, the transaction acquires all the locks. The third phase is started as
soon as the transaction releases its first lock.
o In the third phase, the transaction cannot demand any new locks. It only releases the
acquired locks.
There are two phases of 2PL:

Growing phase: In the growing phase, a new lock on the data item may be acquired by
the transaction, but none can be released.

Shrinking phase: In the shrinking phase, existing lock held by the transaction may be
released, but no new locks can be acquired.

In the below example, if lock conversion is allowed then the following phase can
happen:

1. Upgrading of lock (from S(a) to X (a)) is allowed in growing phase.


2. Downgrading of lock (from X(a) to S(a)) must be done in shrinking phase.

Example:
The following way shows how unlocking and locking work with 2-PL.

Transaction T1:

o Growing phase: from step 1-3


o Shrinking phase: from step 5-7
o Lock point: at 3

Transaction T2:

o Growing phase: from step 2-6


o Shrinking phase: from step 8-9
o Lock point: at 6

4. Strict Two-phase locking (Strict-2PL)

o The first phase of Strict-2PL is similar to 2PL. In the first phase, after acquiring all the
locks, the transaction continues to execute normally.
o The only difference between 2PL and strict 2PL is that Strict-2PL does not release a lock
after using it.
o Strict-2PL waits until the whole transaction to commit, and then it releases all the locks at
a time.
o Strict-2PL protocol does not have shrinking phase of lock release.

It does not have cascading abort as 2PL does.

Starvation

Starvation is the situation when a transaction needs to wait for an indefinite


period to acquire a lock.

Following are the reasons for Starvation:

 When waiting scheme for locked items is not properly managed


 In the case of resource leak
 The same transaction is selected as a victim repeatedly
Deadlock

Deadlock refers to a specific situation where two or more processes are


waiting for each other to release a resource or more than two processes are
waiting for the resource in a circular chain.

Data independence
1. Logical Data Independence
o Logical data independence refers characteristic of being able to change the conceptual
schema without having to change the external schema.
o Logical data independence is used to separate the external level from the conceptual
view.
o If we do any changes in the conceptual view of the data, then the user view of the data
would not be affected.
o Logical data independence occurs at the user interface level.

2. Physical Data Independence


o Physical data independence can be defined as the capacity to change the internal
schema without having to change the conceptual schema.
o If we do any changes in the storage size of the database system server, then the
Conceptual structure of the database will not be affected.
o Physical data independence is used to separate conceptual levels from the internal levels.
o Physical data independence occurs at the logical interface level.
Fig: Data Independence

Logica Data Independence Physical Data Independence

Logical Data Independence is mainly Mainly concerned with the storage of the
concerned with the structure or changing data.
the data definition.

It is difficult as the retrieving of data is It is easy to retrieve.


mainly dependent on the logical structure
of data.
Compared to Logic Physical Compared to Logical Independence it is
independence it is difficult to achieve easy to achieve physical data
logical data independence. independence.

You need to make changes in the A change in the physical level usually
Application program if new fields are does not need change at the Application
added or deleted from the database. program level.

Modification at the logical levels is Modifications made at the internal levels


significant whenever the logical structures may or may not be needed to improve
of the database are changed. the performance of the structure.

Concerned with conceptual schema Concerned with internal schema

Example: Add/Modify/Delete a new Example: change in compression


attribute techniques, hashing algorithms, storage
devices, etc

Importance of Data Independence


 Helps you to improve the quality of the data
 Database system maintenance becomes affordable
 Enforcement of standards and improvement in database security
 You don't need to alter data structure in application programs
 Permit developers to focus on the general structure of the Database
rather than worrying about the internal implementation
 It allows you to improve state which is undamaged or undivided
 Database incongruity is vastly reduced.
 Easily make modifications in the physical level is needed to improve the
performance of the system.

Role and Duties of Database Administrator (DBA) :


 Decides hardware –
They decides economical hardware, based upon cost, performance and
efficiency of hardware, and best suits organisation. It is hardware which is
interface between end users and database.
 Manages data integrity and security –
Data integrity need to be checked and managed accurately as it protects and
restricts data from unauthorized use. DBA eyes on relationship within data to
maintain data integrity.
 Database design –
DBA is held responsible and accountable for logical, physical design,
external model design, and integrity and security control.
 Database implementation –
DBA implements DBMS and checks database loading at time of its
implementation.
 Query processing performance –
DBA enhances query processing by improving their speed, performance and
accuracy.
 Tuning Database Performance –
If user is not able to get data speedily and accurately then it may loss
organization business. So by tuning SQL commands DBA can enhance
performance of database.

DATABASE OVERALL STRUCTURE

1. Query Processor : 
It interprets the requests (queries) received from end user via an application
program into instructions. It also executes the user request which is received
from the DML compiler. 
Query Processor contains the following components – 
 
 DML Compiler – 
It processes the DML statements into low level instruction (machine
language), so that they can be executed. 
 
 DDL Interpreter – 
It processes the DDL statements into a set of table containing meta data
(data about data). 
 
 Embedded DML Pre-compiler – 
It processes DML statements embedded in an application program into
procedural calls. 
 
 Query Optimizer – 
It executes the instruction generated by DML Compiler. 
 
2. Storage Manager : 
Storage Manager is a program that provides an interface between the data
stored in the database and the queries received. It is also known as Database
Control System. It maintains the consistency and integrity of the database by
applying the constraints and executes the DCL statements. It is responsible for
updating, storing, deleting, and retrieving data in the database. 
It contains the following components – 
 
 Authorization Manager – 
It ensures role-based access control, i.e,. checks whether the particular
person is privileged to perform the requested operation or not. 
 
 Integrity Manager – 
It checks the integrity constraints when the database is modified. 
 
 Transaction Manager – 
It controls concurrent access by performing the operations in a scheduled
way that it receives the transaction. Thus, it ensures that the database
remains in the consistent state before and after the execution of a
transaction. 
 
 File Manager – 
It manages the file space and the data structure used to represent
information in the database. 
 
 Buffer Manager – 
It is responsible for cache memory and the transfer of data between the
secondary storage and main memory. 
 
3. Disk Storage : 
It contains the following components – 
 
 Data Files – 
It stores the data. 
 
 Data Dictionary – 
It contains the information about the structure of any database object. It is
the repository of information that governs the metadata. 
 
 Indices – 
It provides faster retrieval of data item. 
 
SQL | Views
Views in SQL are kind of virtual tables. A view also has rows and columns as they are in
a real table in the database. We can create a view by selecting fields from one or more
tables present in the database. A View can either have all the rows of a table or specific
rows based on certain condition.
In this article we will learn about creating , deleting and updating Views.
We can create View using CREATE VIEW statement. A View can be created
from a single table or multiple tables.

You might also like