Advanced Database Chapter Three
Advanced Database Chapter Three
The DBMS has no inherent way of knowing which database access operations are
It must therefore provide a method to allow the user to indicate the boundaries of
a transaction.
If the data base operations in a transaction do not update the database but
The basic database access operations that Transactions can include are:-
Active, the initial state; the transaction stays in this state while it is executing
Failed, after the discovery that normal execution can no longer proceed
Aborted, after the transaction has been rolled back and the database has been restored to
We say that a transaction has committed only if it has entered the committed state.
Similarly, we say that a transaction has aborted only if it has entered the aborted state.
5 A transaction is said
Prepared BY: Abebe to have terminated if has either committed or aborted.
SH. (MSc.) 12/26/2023
Transaction State
A transaction starts in the active state.
When it finishes its final statement, it
enters the partially committed state.
At this point, the transaction has
completed its execution, but it is still
possible that it may have to be aborted,
since the actual output may still be
temporarily residing in main memory,
and thus a hardware failure may
preclude its successful completion
Figure, State transition diagram for a transaction
A transaction enters the failed state after the system determines that the transaction can no
longer proceed with its normal execution (for example, because of hardware or logical
errors). Such a transaction must be rolled back. Then, it enters the aborted state.
6 Prepared BY: Abebe SH. (MSc.) 12/26/2023
Properties of Transactions
There are properties that all transactions should possess. The four basic, or so-called
A. Atomicity
– The ‘all or nothing’ property. A transaction is an indivisible unit that is either performed
B. Consistency
– A transaction must transform the database from one consistent state to another consistent
state.
It Prepared
is theBY: Abebe
responsibility
SH. (MSc.)
of both the DBMS and the application developers to ensure
7 12/26/2023
Properties of Transactions
C. Isolation.
D. Durability.
However, when two or more users are accessing the database simultaneously
and at least one is updating data, there may be interference that can result in
inconsistencies.
Reduced waiting time:- There may be a mix of transactions running on a system, some
short and some long.
If transactions run serially, a short transaction may have to wait for a preceding long
transaction to complete, which can lead to unpredictable delays in running a transaction.
Moreover, it also Reduces the average response time of transaction :the average time for a
transaction to be completed after it has been submitted.
11 Prepared BY: Abebe SH. (MSc.) 12/26/2023
Problems arising due to concurrency
Concurrency is of great advantage because of the above said reasons but when
there is multiple concurrent access to a single file in the database, then many
problems would arise in a DBMS.
On the other hand, transaction T2 Transfer 200 birr from account B to account C. The
transactions) are= T!
Non serial schedules= total schedules- serial schedules
execute concurrently without interfering with one another, and thereby produce a
database state that could be produced by a serial execution.
Notice
There are n! possible serial schedules of n transactions and many more possible non-
serial schedules. We can form two disjoint groups of the non-serial schedules:
Those that are equivalent to one (or more) of the serial schedules, and hence are
serializable; and
Those that are not equivalent to any serial schedule and hence are not serializable.
y =2.
A. how many possible total, serial and non serial number of schedules are there?
B. Create all possible serial schedules and examine the values of x and y;
C. Create some non-serial schedule and examine the values of x and y. identify
26
serializable schedules?
Prepared BY: Abebe SH. (MSc.) 12/26/2023
Serializability
When transactions are running concurrently the operations may conflict each other. Such operations
conflicting equivalent?
Schedule 1
Schedule 2
The algorithm looks at only the read_item and write_item operations in a schedule to
construct a precedence graph (or serialization graph), which is a directed graph G=(N,E)
that consists of a set of nodes N and a set of directed edges E. which is constructed as
follows:
1. Create a node for each transaction.
2. Create a directed edge Ti →Tj, if Tj reads the value of an item written by Ti.
3. Create a directed edge Ti →Tj, if Tj writes a value into an item after it has been read by Ti.
4. Create a directed edge Ti →Tj, if Tj writes a value into an item after it has been written by
Ti.
5. The schedule S is conflict serializable if and only if the precedence graph has no cycles.
33 Prepared BY: Abebe SH. (MSc.) 12/26/2023
Testing for Conflict Serializability of a schedule
If there is no cycle in the precedence graph, we can create an equivalent serial schedule
However, if the precedence graph has a cycle, it is easy to show that we can not create
Example1: draw precedence graph for the following schedule and check whether it is
conflict serializable?
S= r1(x); r2(x);w1(x);r1(y);w2(x);w1(y).
schedule.
Note
schedule.
Non-recoverable Schedule.
A. Recoverable schedule
B. Non-recoverable schedule
I. Cascading Schedule
II. Cascadeless Schedule
III. Strict Schedule
data item, until and unless the last transaction that has
written is committed/aborted, then such a schedule is
called as Cascadeless Schedule.
and commits before the transaction from where it has read the value, then such a
schedule is called Non-Recoverable schedule.
A non recoverable schedule means when there is a system failure, we may not be
If the commit operation of Ti doesn't occur before the commit operation of Tj, it is
non-recoverable.
transactions.
Resolve read-write and write-read conflicts.
There are two main concurrency control techniques that allow transactions to execute
Timestamp methods.
is accessing the database, a lock may deny access to other transactions to prevent
incorrect results.
A lock is a variable associated with a data item that describes the status of the item with
Several types of locks are used in concurrency control. Some of them are;
Binary Locks
Two operations, lock_item and unlock_item, are used with binary locking.
A transaction requests access to an item X by first issuing a lock_item(X)
operation.
If LOCK(X) =1, the transaction is forced to wait.
If LOCK(X) =0, it is set to 1 (the transaction locks the item) and the transaction is allowed to access
item X.
When the transaction is through using the item, it issues an unlock_item (X) operation, which sets
Prepared BY: Abebe SH. (MSc.)
50 LOCK(X) 12/26/2023
to 0 (unlocks the item) so that X may be accessed by other transactions.
Binary Locks
When the binary locking scheme is used, every transaction must obey the following
rules:
– A transaction T must issue the operation lock_item(X) before any read_item(X) or
item X.
– A transaction T will not issue an unlock_item(X) operation unless it already holds the
lock on item X.
51 Prepared BY: Abebe SH. (MSc.) 12/26/2023
Shared/ Exclusive Locks
The preceding binary locking scheme is too restrictive for database items, because at most one
transaction can hold a lock on a given item.
We should allow several transactions to access the same item X if they all access X for
reading purposes only.
However, if a transaction is to write an item X, it must have exclusive access to X.
For this purpose, a different type of lock called shared/ Exclusive lock is used.
In shared/ exclusive locking method there are two locks. These are
Shared Lock (S):- If a transaction has a shared lock on a data item, it can read the item but
not update it. and
Exclusive Lock (X):- If a transaction has an exclusive lock on a data item, it can both read
and update the item.
52 Prepared BY: Abebe SH. (MSc.) 12/26/2023
Shared/ Exclusive Locks
Since read operations cannot conflict, it is permissible for more than one transaction to hold
On the other hand, an exclusive lock gives a transaction exclusive access to that item. Thus, as
long as a transaction holds the exclusive lock on the item, no other transactions can read or update
that data item.
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, the DBMS determines whether the request is compatible with the
existing lock. If a shared lock is requested on an item that already has a shared lock on it, the
53 Prepared BY: Abebe SH. (MSc.) 12/26/2023
request will be granted; otherwise, the transaction must wait until the existing lock is released.
Shared/ Exclusive Locks
The following table (compatibility matrix) shows which type of lock requests can be
granted simultaneously.
Growing Phase:- This phase is also known as the first phase or the expanding
phase.
– In this phase that the transaction acquires all the locks needed by it but it cannot
phase.
– Here a transaction is not allowed to acquire any new locks but it
55 Prepared BY: Abebe SH. (MSc.) 12/26/2023
can release the existing locks it holds.
Two-Phase Locking (2-PL)
Let’s see the transaction implementing 2PL
In this variation, a transaction T does not release any of its exclusive (write) locks until after
it commits or aborts.
Hence, no other transaction can read or write an item that is written by T unless T has
terminated (Committed/ Aborted).
Strict 2-PL is not dead lock-free
Rigorous 2-PL
In this variation, a transaction T does not release any of its locks (exclusive or shared) until
after it commits or aborts, and so it is easier to implement than strict 2pL.
58 Prepared BY: Abebe SH. (MSc.) 12/26/2023
Types Of Two-Phase Locking (2PL)
Conservative 2-PL (also called Static 2-PL)
Requires a transaction to lock all the items it accesses before the transaction begins execution, by
predeclaring its read-set and write-set.
the read-set of a transaction is the set of all items that the transaction reads, and
If any of the predeclared items needed cannot be locked, the transaction does not lock any item;
instead, it waits until all the items are available for locking.
Conservative 2PLis a deadlock-free protocol
The deadlock avoidance method is suitable for smaller databases whereas the
Methods like "wait-for graph" are available but they are suitable for only