Unit-4
Unit-4
Unit-4
1
TRANSACTION
1 Transaction
• A transaction is a unit of program execution that accesses and possibly updates
various data items.
Example: Suppose an employee of bank transfers Rs. 800 from X’s account to Y’s
account. This small transaction contains several low-level tasks:
X’s Account:
Open Account(X)
Old Balance = X.balance
New Balance = Old Balance - 800
X.balance = New Balance
Close Account(X)
Y’s Account:
Open Account(Y)
Old Balance = Y.balance
New Balance = Old Balance + 800
Y.balance = New Balance
Close Account(Y)
Operations of Transaction:
Read(X): Read operation is used to read the value of X from the database and stores
it in a buffer in main memory.
Write(X): Write operation is used to write the value back to the database from the
buffer.
Let’s take an example to debit transaction from an account which consists of follow-
ing operations:
1. Read(X);
2. X = X - 500;
3. Write(X);
2
• The first operation reads X’s value from database and stores it in a buffer.
• The second operation will decrease the value of X by 500. So buffer will contain
3500.
• The third operation will write the buffer’s value to the database. So X’s final value
will be 3500.
But it may be possible that because of the failure of hardware, software or power, etc.
that transaction may fail before finished all the operations in the set.
For example: If in the above transaction, the debit transaction fails after execut-
ing operation 2 then X’s value will remain 4000 in the database which is not acceptable
by the bank.
2 Properties of Transaction
To ensure integrity of the data, we require that the database system maintain the following
properties of the transactions:
1. Atomicity: Either all operations of the transaction are reflected properly in the
database, or none are.
2. Consistency:
• This means that integrity constraints must be maintained so that the database
is consistent before and after the transaction. It refers to the correctness of a
database.
• Execution of a transaction in isolation (that is, with no other transaction
executing concurrently) preserves the consistency of the database.
3. Isolation:
• In isolation, if the transaction T1 is being executed and using the data item
X, then that data item can’t be accessed by any other transaction T2 until
the transaction T1 ends.
• It shows that the data which is used at the time of execution of a transaction
cannot be used by the second transaction until the first one is completed.
3
These properties are often called the ACID properties.
Ti: read(A);
A := A - 50;
write(A);
read(B);
B := B + 50;
write(B).
3 Transaction State
A transaction must be in one of the following states:
• Active: This is the initial state. The transaction stays in this state while it is
executing.
• Partially committed: Transaction enter into this state after the final statement
has been executed.
• Failed: Transaction enter into this state after the discovery that normal execution
can no longer proceed.
• Aborted: Transaction enter into this state after the transaction has been rolled
back and the database has been restored to its state prior to the start of the trans-
action.
• It can restart the transaction, but only if the transaction was aborted as a result
of some hardware or software error that was not created through the internal logic
of the transaction. A restarted transaction is considered to be a new transaction.
• It can kill the transaction. It usually does so because of some internal logical error
that can be corrected only by rewriting the application program, or because the
input was bad, or because the desired data were not found in the database.
4 Schedule
A schedule is a sequence of instructions of all the transactions in which order these in-
structions will execute.
There are two types of schedule. (1) Serial schedule (2) Concurrent schedule
4
Serial schedule: The serial schedule is a type of schedule where one transaction is
executed completely before starting another transaction. In the serial schedule, when the
first transaction completes its cycle, then the next transaction is executed. Concurrent
schedule: If interleaving of operations is allowed, then the schedule will be concurrent
schedule.
T1: read(A);
A := A - 50;
write(A);
read(B);
B := B + 50;
write(B).
T2: read(A);
temp := A * 0.1;
A := A - temp;
write(A);
read(B);
B := B + temp;
write(B).
5 Serializability
To ensure consistency of database system, we must make a serializable schedule. Here,
we will study two types of Serializability. These are (1) Conflict Serializability (2) View
Serializability.
5
6
7
(2) Ii = write(Q), and Ij = read(Q).
(3) Ii = write(Q), and Ij = write(Q).
8
9
In this situation, this schedule can not be transformed into any serial schedule. Therefore,
the schedule-5 is non-conflict serializable.
2. For each data item Q, if transaction Ti executes read(Q) in schedule S, and if that
value was produced by a write(Q) operation executed by transaction Tj , then the
read(Q) operation of transaction Ti must, in schedule S’, also read the value of Q
that was produced by the same write(Q) operation of transaction Tj .
3. For each data item Q, the transaction (if any) that performs the final write(Q)
operation in schedule S must perform the final write(Q) operation in schedule S’.
View serializable: A schedule S is said to be view serizaliabe if it is view equivalent
to a serial schedule.
10
11
Now, we will check all the three conditions for data item B.
Clearly, in schedule-3 and schedule-1, transaction T1 reads the initial value of B, therefore
condition-1 is satisfied for data item B.
Clearly, in schedule-3, transaction T2 reads the value of B written by T1 . In schedule-1,
transaction T2 reads the value of B written by T1 . Clearly same order of write-read occur
in both schedule, therefore second condition is also satisfied.
Clearly, in schedule-3, final write(B) operation is performed by transaction T2 . In schedule-
1, final write(B) operation is also performed by transaction T2 . Therefore, third condition
is also satisfied.
Clearly all the three conditions are satisfied for data item B.
Since all the three conditions are satisfied for each data item in both schedules 1 and 3,
therefore both schedule-3 is view equivalent to serial schedule-1. Therefore, schedule-3 is
view serializable.‘
Now, we check the view equivalent of schedule-4 with serial schedule-2 i.e. T2 T1 .
First consider data item A.
Clearly, in schedule-4, transaction T1 reads the initial value of A, but in serial schedule-2,
transaction T2 reads the initial value of A. Therefore condition-1 is not satisfied for data
item A.
Therefore, schedule-4 is not view equivalent to a serial schedule T1 T2 .
Therefore, schedule-4 is not view serializable.‘
12
Consider a schedule S. We construct a directed graph, called a precedence graph,
from S. This graph consists of a pair G = (V, E), where V is a set of vertices and E
is a set of edges. The set of vertices consists of all the transactions participating in the
schedule. The set of edges consists of all edges Ti → Tj for which one of three conditions
holds:
If the precedence graph for S has a cycle, then schedule S is not conflict serializable. If
the graph contains no cycles, then the schedule S is conflict serializable.
Note: If a schedule is conflict serializable then it is also view serializable. But converse
need not be true.
7 Recoverability
If a transaction Ti fails, for whatever reason, we need to undo the effect of this transaction
to ensure the atomicity property of the transaction. In a system that allows concurrent
execution, it is necessary also to ensure that any transaction Tj that is dependent on Ti
(that is, Tj has read data written by Ti ) is also aborted. To achieve this surety, we need
to place restrictions on the type of schedules permitted in the system.
In the following sections, we will study two schedules which are acceptable from the
viewpoint of recovery from transaction failure.
13
14
15
7.1 Recoverable Schedules
A schedule is said to be recoverable schedule if for each pair of transactions Ti and Tj such
that Tj reads a data item previously written by Ti , the commit operation of Ti appears
before the commit operation of Tj .
Solution:
Clearly, in this schedule transaction T11 reads the value of A written by transaction T10
and T10 commits before the read operation of T11 .
Similarly, transaction T12 reads the value of A written by transaction T11 and T11 commits
before the read operation of T12 .
Therefore this schedule is cascadeless schedule.
Note: Every cascadeless schedule is also recoverable schedule. But converse need
not be true.
8 Exercise
1. Consider the following two transactions:
T1 : read(A);
read(B);
if A = 0 then B := B + 1;
write(B)
T2 : read(B);
read(A);
if B = 0 then A := A + 1;
write(A)
Let the consistency requirement be A = 0 ∨ B = 0, with A = B = 0 the initial
values.
(a) Show that every serial execution involving these two transactions preserves the
consistency of the database.
(b) Show a concurrent execution of T1 and T2 that produces a non-serializable
schedule.
16
(c) Is there a concurrent execution of T1 and T2 that produces a serializable sched-
ule?
3. Consider the three transactions T1 , T2 , and T3 , and the schedules S1 and S2 given be-
low. Draw the serializability (precedence) graphs for S1 and S2 and state whether
each schedule is serializable or not. If a schedule is serializable, write down the
equivalent serial schedule(s).
4. What is schedule? What are its types? Explain view serializable and cascadeless
schedule with suitable example of each.
5. Which of the following schedules are conflicts serializable? For each serializable
schedule find the equivalent serial schedule.
S1 : r1 (x); r3 (x); w3 (x); w1 (x); r2 (x)
S2 : r3 (x); r2 (x); w3 (x); r1 (x); w1 (x)
S3 : r1 (x); r2 (x); r3 (y); w1 (x); r2 (z); r2 (y); w2 (y)
7. Define schedule.
8. What do you mean by serializability? Discuss the conflict and view serialzability
with example. Discuss the testing of serializability also.
9. What do you mean by Transaction? Explain transaction property with detail and
suitable example.
17
11. State the properties of transaction.
12. What is transaction? Draw a state diagram of a transaction showing its state.
Explain ACID properties of a transaction with suitable examples.
13. What are the schedules? What are the differences between conflict serialzability
and view serialzability ? Explain with suitable examples what are cascadeless and
recoverable schedules?
18
19