Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

TRANSACTION

MANAGEMENT
CONCEPT OF A TRANSACTION

➢ Collection of operations that form a single


logical unit of work are called transaction.

➢ A transaction is defined as a logical unit of work


which involves a sequence of steps.
EXAMPLE OF A TRANSACTION

Transaction to transfer Rs.50 from account A to account B

single
logical
unit of
work
read(A)

A := A – 50

write(A) Operations
Transaction
read(B)

B := B + 50

write(B)
TRANSACTION STATES

Partially
committed
commited

Active Terminated

Failed Aborted
PROPERTIES OF TRANSACTION(ACID)

➢ Atomicity

➢ Consistency

➢ Isolation

➢ Durability
ATOMICITY

➢ Transaction must be treated


as an a atomic unit. 0%
read(A)

A := A – 50
➢ Either execute 0% or 100%. write(A)
FAIL
read(B)

B := B + 50
➢ In this transaction, if Rs.50 write(B)
is deducted from account A
then it must be added to 100%
account B
CONSISTENCY

A=500,B=500
➢ Transaction must A+B=1000
remain in a consistent
read(A)
state after any
transaction. A := A – 50

write(A)

read(B)
➢ In this transaction,
total of A and B must B := B + 50
remain same before write(B)
and after the execution
of transaction A=450,B=550
A+B=1000
ISOLATION

➢ Changes occurring in a
particular transaction
will not be visible to any read(A)
other transaction until it
has been committed A := A – 50

write(A)
➢ In this transaction, once
read(B)
transaction starts from
first step(step1) it’s result B := B + 50
should not be access by
any other transaction write(B)
until last step(step6) is
completed
DURABILITY

➢ Changes made by A=500,B=500


transaction persist
even if there are read(A)
system failures.
A := A – 50

write(A)

➢ In this transaction, read(B)


once transaction B := B + 50
completed up to last
step(step6) it’s result write(B)
must be stored
permanently. A=450,B=550
FORMAT OF TRANSACTION

Begin Transaction
(details of the transaction)
Commit
TRANSACTION TO TRANSFER10 PERCENT OF
BALANCE FROM ACCOUNT A TO ACCOUNT B

read(A)
Temp = A *0.1
A = A – Temp
write(A)
read(B)
B = B + Temp
write(B)
SCHEDULE
 Schedule – a sequences of instructions that
specify the chronological order in which
instructions of concurrent transactions are
executed.
EXAMPLE OF SCHEDULE

T1 T2 A=B=1000
read(A) read(1000)
A := A – 50 A := 1000 – 50
Write (A) Write (950)
read(B) read(1000)
B := B + 50 B := 1000+ 50
Write (B) Write (1050)
Commit read(A) Commit
Temp = A *0.1 read(950)
A = A – Temp Temp = 950*0.1
write(A) A = 950 – 95
read(B) Write(855)
B = B + Temp read(1050)
write(B) B = 1050+95
commit write(1145)
‘ commit
EXAMPLE OF SCHEDULE

T1 T2 A=B=1000
read(A) read(1000)
Temp = A *0.1 Temp = 1000*0.1
A = A – Temp A = 1000 – 100
write(A) Write(900)
read(B) read(1000)
B = B + Temp B = 1000+100
write(B) write(1100)
commit Commit
read(A) ‘ read(900)
A := A – 50 A := 900 – 50
Write (A) Write (850)
read(B) read(1100)
B := B + 50 B := 1100+ 50
Write (B) Write (1150)
Commit Commit
SERIAL SCHEDULE

 Serial schedule is one in which no transaction


starts until a running transaction has ended.

 Transactions are executed one after the other.


SERIAL SCHEDULE 1 IN WHICH T1 IS
FOLLOWED BY T2

T1 T2
A SERIAL SCHEDULE 2 IN WHICH T2 IS
FOLLOWED BY T1

T2 T1
CONCURRENT SCHEDULE

Not a serial schedule, but it is equivalent to Schedule 1.

T1 T2
EQUIVALENT SCHEDULE
 If two schedule produce the same result after
execution , they are said to be equivalent
schedule.
SERIALIZABILITY
 A schedule is serializable if it is equivalent to a
serial schedule.
 In serial schedules , only one transaction is
allowed to execute at a time.i.e.no concurrency
 In serializable schedules , multiple transactions
can execute simultaneously . i.e. concurrency is
allowed
SERIALIZABILITY
T1 T2
T1 T2
read(A)
R(A)
A := A – 50 R(A)
Write (A)
read(B) W(A)
B := B + 50
Write (B) W(A)
Commit

read(A)
Temp = A *0.1
A = A – Temp
write(A)
read(B)
B = B + Temp T1 T2
write(B)
commit

T1 → T2
OR
T1 T2
T2 → T1
SERIALIZABILITY
 Types
1. Conflict serializability
2. View serializability

Basic Assumption – Each transaction preserves


database consistency.
CONFLICT SERIALIZABILITY
 If a schedule S can be transformed into a
schedule S´ by a series of swaps of non-
conflicting instructions, we say that S and S´ are
conflict equivalent.
 We say that a schedule S is conflict
serializable if it is conflict equivalent to a serial
schedule
CONFLICTING INSTRUCTIONS
1. li = read(Q), lj = read(Q).
2. li = read(Q), lj = write(Q).
3. li = write(Q), lj = read(Q).
4. li = write(Q), lj = write(Q).

o Instruction li and lj 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.
o If both the transactions access different data
item then they are not conflict.
CONFLICT SERIALIZABILITY
R->W, W->W, W-> R
Precedence graph ( V ,E)
T1 T2 T3 Check conflict pair in other transaction and
R(X) draw edge.
R(Y) Loop/Cycle
R(X)
T
R(Y) 1 T
R(Z) 2
W(Y)
W(Z) No
R(Z) Loop
T
W(X) 3 ✓Conflict Serializable
W(Z) ✓Serializable
✓Consistance
CONT…
T1->T2->T3
 T1
T2->T1->T3
T2 T3 T2->T3->T1
R(X) T3->T1->T2 IN DEGREE=0
R(Y) T3->T2->T1
R(X) T1->T3->T2
R(Y)
R(Z) T1
 W(Y) T2→T3→T1
T2
W(Z)
R(Z) T
3
W(X)
W(Z)
EXAMPLE 2…
T1 T2 T3
R(A)
R(B)
R(A) A) Give the precedence
R(B) graph.
B) Is s is conflict
R(C)
serializable ?
W(B)
C) Give serializable
W(C) schedule for given
R(C) schedule S.
W(A)
W(C)

Serializable schedule for given schedule S is -T2 →T3→T1


EXAMPLE 3…
T1 T2 T3

R(A)

W(A)

W(A)

W(A)
EXAMPLE 3…
T1 T2 T3

R(A)

W(A)
T1 T2
LOOP
W(A)
Non Conflict Serializable
W(A) T3

Serializable?
VIEW SERIALIZABILITY
100 S S’ 100

T1 T2 T3 T1 T2 T3
R(A) R(A)

W(A) W(A) A=A-40


A=A-40
A=60
A=60
A=A-40 W(A)
W(A) A=A-40
A=20 A=20

A=A-20 W(A) A=A-20


A=0 W(A) A=0

T2
T1
Serializable
T3
T1 T2
R(X)
W(X)
R(X)
W(X)
R(Y)
W(Y)
R(Y)
W(Y)

You might also like