Department of BSCS
Database System
Group Members
Muzammil 11
Areej 03
Urwa 41
Muhamad Bin Atiq 06
Transaction and concurrency control
Transaction
An action, or series of actions, carried out by a single
user or application program, that reads or updates
the contents of the database.
• It is a logical unit of work.
• It may be an entire program, a part of a program, or a
single statement , and it may involve any number of
operations on the
2
EXAMPLE
3
Types of transections
• If it completes successfully, the transaction is said to have committed.
• If the transaction does not execute successfully, the transaction is
aborted.
• PARTIALLY COMMITTED which occurs after the final statement has
been executed. At this point, it may be found that the transaction has
violated serializability or has violated an integrity constraint and the
transaction has to be aborted.
• FAILED. which occurs if the transaction cannot be committed or the
transaction is aborted while in the ACTIVE state, perhaps due to the
user aborting the
4
Properties of Transactions
(ACID properties)
• Atomicity. The “all or nothing” property. It is the responsibil of the
recovery subsystem of the DBMS to ensure atomicity.
• Consistency. A transaction must transform from one consistent
state to another consistent state. It is the responsibility of both the
DBMS and the application developers to ensure consistency.
• Isolation. Transactions execute independently of one another. It is
the responsibility of the concurrency control subsystem.
• Durability. The effects of a successfully completed (committed)
transaction are permanently recorded in the database. It is the 5
responsibility of the recovery subsystem to ensure durability.
Transection log
To keep track of database transactions, the DBMS maintains a special file
called a log (or journal) that contains information about all updates to the
database.
The log may contain the following data:
• Update Log Record
• Compensation Log Record
• Commit Record
• Abort Record
• Checkpoint Record
• Completion Record
6
Transection log(Continue)
• Update Log Record an update (change) to the database.
It includes this extra information:
PageID : A reference to the Page ID of the modified page.
Length and Offset: Length in bytes and offset of the page are
usually included.
Before and After Images: Includes the value of the bytes of page
before and after the page change. Some databases may have logs
which include one or both images.
7
Transection log(Continue)
• Compensation Log Record (CLR) notes the rollback of a
particular change to the database. It includes this extra
information:
undoNextLSN: This field contains the LSN(log serial no) of the next log
record that is to be undone for transaction that wrote the last Update Log.
• Commit Record notes a decision to commit a transaction.
• Abort Record notes a decision to abort and hence roll back a
transaction.
8
8
Transection log(Continue)
• Checkpoint Record notes that a checkpoint has been made. These are
used to speed up recovery. They record information that eliminates the
need to read a long way into the log's past.
redoLSN: This is a reference to the first log record that
corresponds to a dirty page.
undoLSN: This is a reference to the oldest log record of the oldest
in-progress transaction.
• Completion Record notes that all work has been done for this
particular transaction.
9
Time to call
Areej
Roll no 03 10
Concurrency Control
The process of managing simultaneous operations on the
database without having them interfere with one another.
The Need for Concurrency Control
• Two or more users are accessing the database simultaneously and at
least one is updating data.
• Transactions interleave
• To solve Lost update problem
• To solve Uncommitted dependency problem (or dirty read)
• To solve Inconsistent analysis problem.
11
Lost update problem
• An apparently successfully completed update operation by
one user can be overridden by another user. This is known
as the lost update problem and is illustrated in Figure.
12
The uncommitted dependency
problem
The uncommitted dependency problem occurs when one
transaction is allowed to see the intermediate results of another
transaction before it has committed. Figure shows an example of
an uncommitted dependency that causes an error.
13
The inconsistent analysis
problem`
The problem of inconsistent analysis occurs
when a transaction reads several values from the
database but a second transaction updates some of
them during the execution of the first.
14
Fuzzy read problem
• Another problem can occur when a transaction T rereads
a data item it has previously read but, in between, another
transaction has modified it. Thus, T receives two different
values for the same data item. This is sometimes referred
to as a nonrepeatable (or fuzzy) read.
15
Time to call
Muhammad Bin
Atiq(MBA)
Roll no 06 16
Concurrency Control
Methods
To solve these problems are the basic methods
• Looking method
• Time span method
• Optimistic techniques
17
Locking Methods
A procedure used to control concurrent access to data. When
one transaction is accessing the database, a lock may deny
access to other transactions to prevent incorrect results.
These are the main two types of look.
• Shared lock :If a transaction has a shared lock on a data
item, it can read the item but not update it.
• Exclusive lock :If a transaction has an exclusive lock on a
data item, it can both read and update the item.
18
Locking Methods
• Can upgrade the lock to an exclusive lock.
• Can downgrade the lock to a shared lock.
• Any transaction that needs to access a data item must first lock the item,
requesting a shared lock for read-only access or an exclusive lock for both read
and write access.
• If the item is not already locked by another transaction, the lock will be
granted.
• If the item is currently locked transaction can be granted or leave for wait.
• A transaction continues to hold a lock until it explicitly releases it either
during execution or when it terminates (aborts or commits).
19
Locking Methods (cont..)
Granularity
The size of data items chosen as the unit of protection by a concurrency
control protocol.
These are the levels of Granularity.
• Database Level
• File Level
• Page Level
• Recode Level
• Field Level
20
Locking Methods (cont..)
Hierarchy of granularity
• The root node represents the entire database
• The level 1 nodes represent files
• The level 2 nodes represent pages
• The level 3 nodes represent records
• The level 4 leaves represent individual fields
21
Locking Methods (cont..)
Multiple-granularity locking
• This strategy uses a new type of lock called an intention
lock. When any node is locked, an intention lock is placed
on all the ancestors of the node.
Intention locks may be either
• Intention shared (IS) lock
• intention exclusive (IX) lock
22
Locking Methods (cont..)
Two-phase locking (2PL)
A transaction follows the two-phase locking protocol if all locking
operations precede the first unlock operation in the transaction.
Transaction can be divided into two phases
• Growing phase in which it acquires all the locks needed
but cannot release any locks .
• shrinking phase, in which it releases its locks but cannot
acquire any new locks.
23
Two-phase locking (2PL)
The rules are:
• A transaction must acquire a lock on an item before
operating on the item. The lock may be read or write,
depending on the type of access needed.
• Once the transaction releases a lock, it can never acquire
any new locks.
Now we try to solve the problems that we have faced .let’s
go !
24
Two-phase locking (2PL)
• Preventing the lost update problem using 2PL
An apparently successfully completed update operation by
one user can be overridden by another user. This is known
as the lost update problem and is illustrated in Figure
Now solve it. Let’s go !
25
Two-phase locking (2PL)
• Preventing the uncommitted dependency problem
using 2PL
The uncommitted dependency problem occurs when one
transaction is allowed to see the intermediate results of
another transaction before it has committed. Figure shows
an example of an uncommitted dependency that causes an
error.
Now solve it. Let’s go !
26
Two-phase locking (2PL)
• Preventing the inconsistent analysis problem using
2PL
The problem of inconsistent analysis occurs when a
transaction reads several values from the database but a
second transaction updates some of them during the
execution of the first.
Now solve it. Let’s go !
27
Two-phase locking (2PL)
(Cascading rollback)
• Cascading rollback
The situation, in which a single transaction leads to a series of rollbacks,
is called cascading rollback.
Now how we can solve this problem ?
Let’s try to solve !
• One way to achieve this with two-phase locking is to leave the release of
all locks until the end of the transaction, This is called rigorous 2PL.
• Another variant of 2PL, called strict 2PL, holds only exclusive locks
until the end of the transaction
28
Deadlock
An impasse that may result when two (or more) transactions are
each waiting for locks to be released that are held by the other.
There are three general techniques for handling deadlock
• Timeouts
• Deadlock prevention
• Deadlock detection and recovery
29
Deadlock
• Timeouts
lock will wait for only a system-defined period of time
• Deadlock prevention
Wait-Die, Wound-Wait, Conservative 2PL(complete looks)
• Deadlock detection
wait-for graph (WFG)(cycle),rollback(Choice of deadlock
victim, How far to roll a transaction back, Avoiding starvation)
30
Timestamping Methods
• Timestamp
A unique identifier created by the DBMS that indicates the
relative starting time of a transaction.
• Timestamping
A concurrency control protocol that orders transactions in such a
way that older transactions, transactions with smaller
timestamps, get priority in the event of conflict.
31
Timestamping Methods
Important points
• Last update on that data item was carried out by an older
transaction.
• There are timestamps for data items
• Read_timestamp
giving the timestamp of the last transaction to read the item.
• Write_timestamp
giving the timestamp of the last transaction to write (update) the item.
32
Timestamping Methods
For a transaction T with timestamp ts(T), the timestamp ordering protocol
works as follows:
• Transaction T issues a read(x)
• ts(T) > write_timestamp(x)
• ts(T) <= write_timestamp(x)
• Transaction T issues a write(x)
• ts(T) > read_timestamp(x)
• ts(T) <= write_timestamp(x)
33
Optimistic techniques
Optimistic techniques are based on the assumption that conflict
is rare and that it is more efficient to allow transactions to proceed
without
imposing delays to ensure serializability.
• Rollback at the point of commit
• There are two or three phases to an optimistic concurrency
control protocol.
• These techniques potentially allow greater concurrency than
traditional protocols, as no locking is required.
34
Optimistic techniques
Phases
(1) All transactions S with earlier timestamps must have
finished before transaction
• Read phase.(stores T started; that is, finish(S)
in local variables)
<start(T).
• Validation
(2) phase.
If transaction (Checks
T starts are performed
before to ensure
an earlier one that
S finishes,
serializability)
then:
(a) the
• Write set of datacopy
phase.(local itemsare
written by to
applied the earlier
the transaction
database)
are not the ones read by the current transaction; and
(b) the earlier transaction completes its write phase
before the current transaction enters its validation phase,
that is, start(T) < finish(S) <validation(T). 35
Database Recovery
The process of restoring the database to a correct state in the event of a failure.
Need
• System crashes due to hardware or software errors, resulting in loss of main memory;
• Media failures, such as head crashes or unreadable media, resulting in the loss of
parts of secondary storage;
• Application software errors, such as logical errors in the program that is accessing
the database, that cause one or more transactions to fail;
• Natural physical disasters, such as fires, floods, earthquakes, or power failures;
• Carelessness or unintentional destruction of data or facilities by operators or users;
• Sabotage, or intentional corruption or destruction of data, hardware, or software
facilities.
36
Transactions and Recovery
• It is the role of the recovery manager.
• To implement the read and write operation use buffer.
• Buffers have been flushed, operations be regarded as
permanent.(triggered or auto)
• Explicit writing of the buffers to secondary storage is
known as force-writing.
• Redo , Undo (rollback), Partial Undo, Global Undo.
37