Module 4 DBMS.
Module 4 DBMS.
Transaction Management
Transaction concept
Transaction states
ACID properties
Schedules
Serializability
• Conflict serializability
• View serializability
Recoverability
Concurrency control
• Lock based protocols
• Timestamp-based protocols
Deadlocks handling
SQL stored procedures
Transaction Management
Transaction Management is a crucial concept in Database Management Systems (DBMS) that
ensures the integrity, consistency, and reliability of data in a multi-user environment.
A transaction is a sequence of one or more SQL operations (like insert, update, delete)
performed as a single logical unit of work.
Transaction concept
Transaction refers to execution of any one user program in dbms.
(Or)
Transaction can be defined as group of tasks being executed.
(Or)
Transaction also referred to as an event that which occurs on a database with read/write
operation.
Basic transaction operations:
➢ read (X): Performs the reading operation of data item X from the database.
➢ write (X): Performs the writing operation of data item X to the database.
Note: During transaction execution the database may be inconsistent but when the
transaction is committed, the database must be consistent.
Two main issues to deal with:
• Failures, (e.g. hardware failures and system crashes)
• Concurrency, for simultaneous execution of multiple transaction
Transaction states
Every transaction undergoes several states in its execution.
A transaction can be in any one of the following states:
1. Start
2. Partially Committed
3. Committed
4. Failed
5. Aborted Or Terminate
Active - This is the first state of transaction and here the transaction is being executed. For
example, updating or inserting or deleting a record is done here. But it is still not saved to the
database. When we say transaction it will have set of small steps, and those steps will be
executed here.
Partially Committed - This is also an execution phase where last step in the transaction is
executed. But data is still not saved to the database. In example of calculating total marks,
final display the total marks step is executed in this state.
Committed - In this state, all the transactions are permanently saved to the database. This
step is the last step of a transaction, if it executes without fail.
Failed - If a transaction cannot proceed to the execution state because of the failure of the
system or database, then the transaction is said to be in failed state. In the total mark
calculation example, if the database is not able fire a query to fetch the marks, i.e.; very first
step of transaction, then the transaction will fail to execute.
Aborted - If a transaction is failed to execute, then the database recovery system will make
sure that the database is in its previous consistent state. If not, it brings the database to
consistent state by aborting or rolling back the transaction. If the transaction fails in the middle
of the transaction, all the executed transactions are rolled back to it consistent state before
executing the transaction. Once the transaction is aborted it is either restarted to execute
again or fully killed by the DBMS.
ACID properties
To ensure consistency, completeness of the database in scenario of concurrent access, system
failure, the following ACID properties can be enforced on to database.
1. Atomicity,
2. Consistency,
3. Isolation and
4. Durability
Atomicity: This property states that all the instructions with in a transaction must be executed
or none of them should be executed.
➢ This property states that all transactions execution must be atomic i.e. all actions
should be carried out or none of the actions should be executed.
It involves following two operations.
➢ Abort: If a transaction aborts, changes made to database are not visible.
➢ Commit: If a transaction commits, changes made are visible.
Atomicity is also known as the ‘All or nothing rule’.
Example: Consider the following transaction T consisting of T1 and T2: Transfer of 100 from
account X to account Y.
If the transaction fails after completion of T1 but before completion of T2 (say, after write(X)
but before write(Y)), then amount has been deducted from X but not added to Y. This results
in an inconsistent database state. Therefore, the transaction must be executed in entirety to
ensure correctness of database state.
Consistency: The database must remain in consistence state even after performing any kind
of transaction ensuring correctness of the database.
➢ If we execute a particular transaction in isolation (or) together with other transaction
in multiprogramming environment. the transaction should give same result in any
case.
➢ Each transaction, run by itself with no concurrent execution of other transactions,
must preserve the consistency of the database. This property is called consistency and
the DBMS assumes that it holds for each transaction. Ensuring this property of a
transaction is the responsibility of the user.
Example: Consider the following transaction T consisting of T1 and T2
Isolation: When executing multiple transactions concurrently & trying to access shared
resources the system should create an order such that the only one transaction can access the
shared resource at the same time & release it after completion of its execution for other
transaction.
➢ This property ensures that multiple transactions can occur concurrently without
leading to inconsistency of database state. Transactions occur independently without
interference.
➢ Changes occurring in a particular transaction will not be visible to any other
transaction until that particular change in that transaction is written to memory or has
been committed.
Note: To achieve isolation, you should use locking mechanism among shared resources.
Suppose T has been executed till Read (Y) and then T’’ starts. As a result, interleaving of
operations takes place due to which T’’ reads correct value of X but incorrect value of Y and
sum computed by T’’: (X+Y = 50, 000+500=50, 500) is thus not consistent with the sum at end
of transaction: T: (X+Y = 50, 000 + 450 = 50, 450).
This results in database inconsistency, due to a loss of 50 units. Hence, transactions must take
place in isolation and changes should be visible only after a they have been made to the main
memory.
Durability: This property states that once after the transaction is completed the changes that
made should be permanent & should be recoverable even after system crash/power failure.
➢ This property ensures that once the transaction has completed execution, the updates
and modifications to the database are stored in and written to disk and they persist
even is system failure occurs. These updates now become permanent and are stored
in a non-volatile memory.
Transaction Schedules
Schedule
➢ It refers to the list of actions to be executed by transaction.
➢ A schedule is a process of grouping the transactions into one and executing them in a
predefined order.
Schedule of actions can be classified into 2 types.
1. Serializable schedule/serial schedule.
2. Concurrent schedule.
1. Serial schedule:
➢ In the serial schedule the transactions are allowed to execute one after the other
ensuring correctness of data.
➢ A schedule is called serial schedule, if the transactions in the schedule are defined to
execute one after the other.
2. Concurrent schedule:
Concurrent schedule allows the transaction to be executed in interleaved manner of
execution.
Complete schedule:
It is a schedule of transactions where each transaction is committed before terminating. The
example is shown below where transactions T1 and T2 terminates after committing the
transactions.
Serializability
Basic Assumption – Each transaction preserves database consistency. Thus, serial execution
of a set of transactions preserves database consistency.
A (possibly concurrent) schedule is serializable if it is equivalent to a serial schedule. Different
forms of schedule equivalence give rise to the notions of:
1. Conflict Serializability
2. View Serializability
Conflict Serializability
A schedule is conflict serializable if it is conflict equivalent to some serial schedule.
Conflict Equivalent: Two schedules are said to be conflict equivalent when one can be
transformed to another by swapping non-conflicting operations.
Conflict Serializable: A schedule is called conflict serializable if it can be transformed into a
serial schedule by swapping non-conflicting operations.
Conflicting operations: Two operations are said to be conflicting if all below conditions are
satisfied:
➢ They belong to different transaction
➢ They operation on same data item
➢ At Least one of them is a write operation
Conflicting operations refers to two instructions of two different transactions may want to
access same data to perform read/write operation.
Conflicting Instructions
Instructions li and lj of transactions Ti and Tj respectively, conflict if and only if there exists
some item Q accessed by both li and lj, and at least one of these instructions write Q
1. li = read(Q), lj = read(Q). li and lj don’t conflict
2. li = read(Q), lj = write(Q). They conflict
3. li = write(Q), lj = read(Q). They conflict
4. li = write(Q), lj = write(Q). They conflict
As precedence graph is having cycles or closed loops, the given schedule is not serializable.
Example: Check whether the given schedule S is conflict serializable or not-
S : R1(A) , R2(A) , R1(B) , R2(B) , R3(B) , W1(A) , W2(B)
Step-01: List all the conflicting operations and determine the dependency between the
transactions-
• R2(A) , W1(A) (T2 → T1)
• R1(B) , W2(B) (T1 → T2)
• R3(B) , W2(B) (T3 → T2)
Step-02: Draw the precedence graph
Clearly, there exists a cycle in the precedence graph. Therefore, the given schedule S is not
conflict serializable.
Step-01: List all the conflicting operations and determine the dependency between the
transactions-
• R4(A) , W2(A) (T4 → T2)
• R3(A) , W2(A) (T3 → T2)
• W1(B) , R3(B) (T1 → T3)
• W1(B) , W2(B) (T1 → T2)
• R3(B) , W2(B) (T3 → T2)
Clearly, there exists no cycle in the precedence graph. Therefore, the given schedule S is
conflict serializable.
Example: A schedule "S" having three transactions t1, t2, and t3 working simultaneously.
Check Whether S is Conflict Serializable or Not?
T1 T2 T3
R(x)
R(y)
R(y)
W(y)
W(x)
W(x)
R(x)
W(x)
Precedence graph
As there is no loop in the graph, it is a conflict serializable schedule as well as a serial
schedule.
Since it is a serial schedule, we can detect the order of transactions as well.
The order of the Transactions: t1 will execute first as there is no incoming edge on T1.
(i.e., In degree=0)
T3 will execute second as it depends on T1 only.
T2 will execute at last as it depends on both T1 and T3.
So, order of its equivalent serial schedule is: T1 --> T3 --> T2
Example: A schedule "S" having three transactions t1, t2, and t3 working simultaneously.
Check Whether S is Conflict Serializable or Not?
T1 T2 T3
R(x)
R(z)
W(z)
R(y)
R(y)
W(y)
W(x)
W(z)
W(x)
Precedence graph
As the cycle is formed, the transactions cannot be serializable.
Example: Check Whether S is Conflict Serializable or Not?
Step-1: List all the conflicting operations and the dependency between the transactions.
₋ W1(B) , W2(B) (T1 → T2)
₋ W1(B) , W3(B) (T1 → T3)
₋ W1(B) , W4(B) (T1 → T4)
₋ W2(B) , W3(B) (T2 → T3)
₋ W2(B) , W4(B) (T2 → T4)
₋ W3(B) , W4(B) (T3 → T4)
Step-2:
• Draw the precedence graph.
Step-3:
• Clearly, there exists no cycle in the precedence graph. Therefore, the given schedule
S is conflict serializable.
Example: Check Whether S is Conflict Serializable or Not?
Step-1:
List all the conflicting operations and determine the dependency between the transactions-
₋ R1(A) , W2(A) (T1 → T2)
₋ R2(A) , W1(A) (T2 → T1)
₋ W1(A) , W2(A) (T1 → T2)
₋ R1(B) , W2(B) (T1 → T2)
₋ R2(B) , W1(B) (T2 → T1)
Step-2:
• Draw the precedence graph.
Step-3:
• Clearly, there exists a cycle in the precedence graph.
Therefore, the given schedule S is not conflict serializable.
View Serializability
This is another type of serializability that can be derived by creating another schedule out of
an existing Schedule.
✓ There may be some schedules that are not Conflict-Serializable but still gives a
consistent result because Conflict-Serializability becomes limited when the
Precedence Graph of a schedule contains a loop/cycle.
✓ But, what if a schedule’s precedence graph contains a cycle/loop and is giving
consistent result.
✓ So, to address such cases we brought the concept of View-Serializability because we
did not want to confine only to Conflict-Serializability.
✓ A schedule is said to be View-Serializable if it is view equivalent to a Serial Schedule
(where no interleaving of transactions is possible).
✓ To check whether a given schedule is view serializable, we need to check whether the
given schedule is View Equivalent or not.
Method 1
View Equivalent
Two schedules T1 and T2 are said to be view equivalent, if they satisfy all the following
conditions:
1. Initial Read: If Ti reads the initial value of object A in S1, it must also read the initial value
of A in S2.
For example, if transaction T1 reads a data item X before transaction T2 in schedule S1 then
In schedule S2, T1 should read X before T2.
Note: Here initial read means the first read operation on a data item, for example, a data item
X can be read multiple times in a schedule but the first read operation on X is called the initial
read.
2. Final Write: If Ti reads a value of A written by Tj in S1, it must also read the value of A
written by Tj in S2.
For example, a data item X is last written by Transaction T1 in schedule S1. In S2, the last
write operation on X should be performed by the transaction T1.
3. Update Read: For each data object A, the transaction (if any) that performs the final write
on A in S1 must also perform the final write on A in S2.
For example, In schedule S1, T3 performs a read operation on A after the write operation on
A by T2 then in S2, T3 should read the A after T2 performs write on A.
Example: Verify whether the below schedules S and S1 are view equivalent or not.
Clearly, there exists a cycle in the dependency graph. Thus, we conclude that the given
schedule S is not view serializable
Example: Check whether the given schedule S is view serializable or not. If yes, then give the
serial schedule.
S : R1(A) , W2(A) , R3(A) , W1(A) , W3(A)
Step-01:
✓ List all the conflicting operations and determine the dependency between the
transactions-
R1(A) , W2(A) (T1 → T2)
R1(A) , W3(A) (T1 → T3)
W2(A) , R3(A) (T2 → T3)
W2(A) , W1(A) (T2 → T1)
W2(A) , W3(A) (T2 → T3)
R3(A) , W1(A) (T3 → T1)
W1(A) , W3(A) (T1 → T3)
Step-02:
• Draw the precedence graph
T2 performs a dirty read operation. The commit operation of T2 is delayed till T1 commits or
roll backs. T1 commits later. T2 is now allowed to commit. In case, T1 would have failed, T2
has a chance to recover by rolling back.
Irrecoverable Schedule: A schedule that cannot be rolled back because of some transactions
already used COMMIT to make the changes permanent in database and these transactions
have used values produced by the failed transactions. These types of schedules are called
irrecoverable schedules.
A transaction performs a dirt read operation from an uncommitted transaction and commits
before the transaction from which it has read the value then such type of schedule is known
as an Irrecoverable Schedule.
Example: Consider the following schedule
Here, T2 performs a dirty read operation. T2 commits before T1. T1 fails later and roll backs.
The value that T2 read now stands to be incorrect. T2 cannot recover since it has already
committed.
The following are the two methods to check whether a schedule is recoverable or
irrecoverable.
Method-01:
All conflict serializable schedules are recoverable. All recoverable schedules may or may not
be conflict serializable.
Example: Check whether the given schedule S is conflict serializable and recoverable or not
Step-01: List all the conflicting operations and determine the dependency between the
transactions-
• R2(X) , W3(X) (T2 → T3)
• R2(X) , W1(X) (T2 → T1)
• W3(X) , W1(X) (T3 → T1)
• W3(X) , R4(X) (T3 → T4)
• W1(X) , R4(X) (T1 → T4)
• W2(Y) , R4(Y) (T2 → T4)
Step-02: Draw the precedence graph
Strict Schedule: If in a schedule, a transaction is neither allowed to read nor write a data item
until the last transaction that has written it is committed or aborted, then such a schedule is
called as a Strict Schedule.
• Strict schedule allows only committed read and write operations.
• Clearly, strict schedule implements more restrictions than cascadeless schedule.
• All strict schedules are cascadeless schedules.
• All cascadeless schedules are not strict schedules.
Concurrency control
In case of concurrent instruction executions to preserve atomicity, isolation and serializability,
we use ‘lock-based’ protocol.
Types of Locks:
1. Binary locks
2. Shared /exclusive locks
Binary Lock − A lock on a data item can be in two states; it is either locked or unlocked.
Shared Lock
✓ which are often denoted as lock-S(), is defined as locks that provide Read-Only access
to the information associated with them.
✓ Whenever a shared lock is used on a database, it can be read by several users, but
these users who are reading the data items will not have permission to edit it or make
any changes to the data items.
✓ A shared lock, also known as a read lock.
Exclusive Lock
✓ This lock allows both the read and write operation, Once this lock is placed on the data
no other lock can be placed on the data until Exclusive lock is released.
✓ The other name for an exclusive lock is write lock.
✓ which are often denoted as X-lock( ) or lock-x( )
Lock Compatibility Matrix
Lock Compatibility Matrix controls whether multiple transactions can acquire locks on
the same resource at the same time.
• If a resource is already locked by another transaction, then a new lock request can be
granted only if the mode of the requested lock is compatible with the mode of the
existing lock.
• Any number of transactions can hold shared locks on an item, but if any transaction
holds an exclusive lock on item, no other transaction may hold any lock on the item.
• compatible locks held by other transactions have been released. Then the lock is
granted.
Upgrading And Downgrading of Locks:
• If a transaction is holding an exclusive lock over an object. It can simply downgrade
from exclusive lock to shared lock after completion of its updation.
• Similarly, a shared lock can be upgraded to exclusive lock on data item. when there
is no other transaction is holding exclusive lock on same data item
Lock Point: The Point at which the growing phase ends, i.e., when a transaction takes the final
lock it needs to carry on its work
Deadlocks handling
Consider two transaction t1 and t2. If t1 holds lock on data item x and t2 holds lock on data
item y now t1 refers lock over y & t2 request lock over x then deadlock situation occur when
none of the transaction are ready to release locks on x, y.
1. wait-die:
In this scheme, if a transaction requests for a resource which is already held with a conflicting
lock by another transaction, then the DBMS simply checks the timestamp of both transactions.
It allows the older transaction to wait until the resource is available for execution.
Let us assume there are two transactions Ti and Tj and let TS(T) is a timestamp of any
transaction T. If T2 holds a lock by some other transaction and T1 is requesting for resources
held by T2 then the following actions are performed by DBMS:
1. Check if TS(Ti) < TS(Tj) - If Ti is the older transaction and Tj has held some resource,
then Ti is allowed to wait until the data-item is available for execution. That means if
the older transaction is waiting for a resource which is locked by the younger
transaction, then the older transaction is allowed to wait for resource until it is
available.
2. Check if TS(Ti) < TS(Tj) - If Ti is older transaction and has held some resource and if Tj
is waiting for it, then Tj is killed and restarted later with the random delay but with the
same timestamp.
For example:
Suppose that transaction T22, T23, T24 have time-stamps 5, 10 and 15 respectively. If
T22requests a data item held by T23 then T22 will wait. If T24 requests a data item held by
T23, then T24 will be rolled back.
If the older transaction has held a resource which is requested by the younger transaction,
then the younger transaction is asked to wait until older releases it.
It is a preemptive technique for deadlock prevention. It is a counterpart to the wait-die
scheme.
When Transaction Ti requests a data item currently held by Tj, Ti is allowed to wait only if it
has a timestamp larger than that of Tj, otherwise Tj is rolled back (Tj is wounded by Ti)
For example:
Suppose that Transactions T22, T23, T24 have time-stamps 5, 10 and 15 respectively. If
T22requests a data item held by T23, then data item will be preempted from T23 and T23 will
be rolled back. If T24 requests a data item held by T23, then T24 will wait.
Here the younger transactions are made to wait in queue& older transaction going to abort.
1. TS(T1) < TS(T2) (Older requests resource from younger) T2 is aborted, T1 gets the resource
2. TS(T1) > TS(T2) (Younger requests resource from older) T1 waits until T2 releases it
Procedures promote reusability and maintainability. Once validated, they can be used in
number of applications. If the definition changes, only the procedure are affected, this
greatly simplifies maintenance.
• OUT: Specifies that the procedure passes a value for this argument back to it’s calling
environment after execution ie. used to return values to a caller of the sub-program.
• INOUT: Specifies that a value for the argument must be specified when calling the
procedure and that procedure passes a value for this argument back to its calling
environment after execution.
• RETURN: It is the data type of the function’s return value because every function
must return a value, this clause is required.
Procedure:
- No return.
- PROCEDURE name IS
Function:
- Returns a value
Note: The AS keyword is used instead of the IS keyword for creating a standalone procedure.
PROCEDURE
Syntax :
• set serveroutput on
DECLARE
r number(10):=2;
a number(10,3):=0;
c number(10,5):=0;
BEGIN
circle_area_circum(r,a,c); --- call to procedure
dbms_output.put_line(r||' '||a||' '||c);
END;
/
Out put:
2 12.56 12.56
FUNCTION
Creating a Function
▪ Both are different forms of PL/SQL blocks, with a declarative executable, and
exception section.
Function Syntax
The syntax for creating a stored function is very similar to the syntax for a procedure
begin
return pfirst||plast; /* returning concatenated name */
end concatenate;
/
Declare
fname varchar2(10):=’hello’;
lname varchar2(10):='world';
coname varchar2(20);
begin
coname:=concatenate(fname,lname);
dbms_output.put_line(coname);
end;
/
Output: helloworld