Adb_CH3
Adb_CH3
Adb_CH3
12/10/24
3-1
Transaction Concept
A transaction is an executing program that performs single
12/10/24
3-3
Properties of Transactions
A Atomicity: a transaction is an atomic unit of processing and it
is either performed entirely or not at all
2. A := A – 50
3. write(A)
4. read(B)
5. B := B + 50
6. write(B)
12/10/24
3-6
Cont…
Write Operation
12/10/24
3-7
Operations of Transaction
Retrieve (Read): to retrieve data stored in a database.
12/10/24
3-9
Cont…
Rollback
it undoes all the updates performed by the SQL statements in
the transaction.
Thus, the database state is restored to what it was before the
first statement of the transaction was executed.
12/10/24
3-10
transaction processing following
these steps
1. Begin a transaction
2. Process database commands
3. Check for error.
If error occurred
rollback the transaction
else
commit the transaction
note: Not able to see the uncommitted.
12/10/24
3-11
States of Transaction
executed.
Failed-When the Normal execution can no longer proceed.
12/10/24
3-12
State Transition Diagram for
Transaction
12/10/24
3-13
Cont…
1. A transaction goes into an active state immediately after it starts execution where it
can issue READ or WRITE operations. When the transaction ends, it moves to the
2. partially committed state where certain recovery protocol ensures that a system
failure will not result in an inability to record the changes of the transaction
permanently.
a. If this check is successful the transaction enters into a commit point and enters the committed state.
If so, all its changes must be recorded permanently in the database.
b. If this check fails, it goes to the failed state.
3. Transaction can go the failed state, from the partially committed state if any of the
checks there fails or if the transaction is aborted from its active state itself.
a. The transaction may then have to be rolled back to undo the effect of WRITE
operations.
4. Committed − If a transaction executes all its operations successfully, it is said to be
committed. And permanently established on the database system.
5. Aborted: If any of the checks fails and the transaction has reached a failed state, then
the recovery manager rolls back all its write operations on the database to bring the
database back to its original state where it was prior to the execution of the
transaction.
12/10/24
3-14
States of Transaction…
A transaction has Committed only if it has entered
the Committed State.
12/10/24
3-15
Concurrency Control
When using single user system there is no need to
manage the transaction but when multiple user
access the same database we need to control the
transaction.
Process of managing simultaneous operations on the
database without having them interfere with one
another is called concurrent control
Prevents interference when two or more users access
database simultaneously and at least one updates
data. Because Interleaving of operations may
produce incorrect results
12/10/24
3-16
Requirements for Database
Consistency
it implies interleaving execution of operations of transaction.
Most DBMS are multi-user systems.
The concurrent execution of many different transactions
submitted by various users must be organized such that
each transaction does not interfere with another
transaction with one another in a way that produces
incorrect results.
The concurrent execution of transactions must be such
that each transaction appears to execute in isolation.
Schedule: the order in which instructions of a transactions
are executed.
Recovery
System failures, either hardware or software, must not
result in an inconsistent database
12/10/24
3-17
Advantages of Concurrent Execution of
Transaction
Improved Throughput
12/10/24
3-18
Potential concurrency
problems:
Potential concurrency problems:
12/10/24
3-21
Cont…
T2 read update but not committed T1-Dirty
T1 T2
R(x)
X=x+20
W(x)
R(x)
X=x+10
W(x)
FAIL
12/10/24
3-23
Cont…
To calc avg salary of emp
R(p)
p=p+100
W(p)
Sum=sum+z
AVG()
12/10/24
3-24
Concurrency control
the process of managing simultaneous operations on the
database without having them interfere with each other.
Schedules Locking
Serial schedules Lock manager
Non-serial 2 Phase Locking
Serializable schedules Deadlocks:
Prevention
Detection
avoidance
12/10/24
3-25
Schedules
write w(x).
If schedule is given by S: r1(x);r2(w);w2(n),r1(y)
The number show the transaction number and x show the database
item.
12/10/24
3-28
Serial Schedules
in which transactions are aligned in such a way that one
transaction is executed first.
When the first transaction completes its cycle, then the next
transaction is executed.
Transactions are ordered one after the other.
EX T1 T2
R1 R2
W1 W2
So we can have the following schedules
R1R2 W1 W2
R1W1 R2W2 - Serial
R2R1 W2W1
R2R1 W1W2
12/10/24
3-29
Example
12/10/24
3-31
Cont…
12/10/24
3-32
Serialisable
is a given set of interleaved transactions is said to be serialisable if
and only if it produces the same results as the serial execution of the
same transactions
Serializability is an important concept associated with locking.
Interleaved Schedule
Serial Schedule
T1 Read(X) T2 Read(X)
T2 Read(X) T2 Read(Y)
T2 Read(Y) T2 Read(Z)
T1 Read(Z)
T1 Read(Y) T1 Read(X)
T2 Read(Z) T1 Read(Z)
T1 Read(Y)
This schedule is serialisable:
12/10/24
3-33
Cont…
12/10/24
3-34
Example of Serial Schedules
SchedulA Schedule B
12/10/24
3-35
Example of Non-serial Schedules
Schedule C Schedule D
T1: T2:
T1: T2:
read_item(X);
read_item(X);
X:= X - N;
X:= X - N;
read_item(X);
write_item(X);
X:= X + M;
read_item(X);
write_item(X);
X:= X + M;
read_item(Y);
write_item(X);
write_item(X);
Y:=Y + N;
read_item(Y);
write_item(Y); Y:=Y + N;
write_item(Y);
12/10/24
3-36
Equivalent Schedules
Result equivalent
Conflict equivalent
View equivalent
12/10/24
3-37
Result Equivalent Schedules
Two schedules are called result equivalent if they produce the
same final state of the database.
Example: check if its result equivalent schedules
A=100
T1 T2
T1 T2
R(A) R(A)
A=A+10 A=A-5
W(A) W(A)
R(A) R(A)
A=A-7 A=A+8
W(A)
W(A)
So
12/10/24 its Equivalent Schedules 3-38
Cont…
Example of result equivalent schedules
A=100
S1 S2
R(A) R(A)
A=A+10 A*1.1
W(A) W(A)
12/10/24
3-39
Conflict operation
Two schedules are said to be conflict equivalent if
the order of any two conflicting operations is the
same in both schedules.
Two operations conflict if :
They belong to different transactions
Access the same data item
At least one of the operation should be a write
operation.
12/10/24
3-40
Conflict operation
if they belong to different transactions
S:R1(x);w2(x) ;R1(y);R2(z) so the conflict is
R1(x) and w2(x) ,w2(x) and r1(y)….
•Must access the same data item
•At least one of the operation should be a write
operation.
T R1(x) W2(x)
Now the conflicting operation pair :
RW
WR
WW but not RR
12/10/24
3-41
Formal definition
Let li and lj be two Instructions of transactions Ti and Tj
respectively. Instructions 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 wrote Q.
1. li = read(Q), lj = read(Q). li and lj don’t conflict.
R(A) R(B)
W(A) R(A)
R(B) W(A)
S1 ⊆S2 because Order should respect R1W2
12/10/24
3-43
Conflict Equivalent
Check for conflict equivalent:
S1:R1(A),w1(A),R2(A),W2(B),R1(B)
S2:R1(A),W1(A),R1(B),R2(B),W2(B)
S1 S2
T1 T2 T1 T2
R(A) R(A)
W(A)
W(A)
R(B)
» R(A)
R(B)
W(B)
W(B)
R(B)
S1:w1(x),R2(z),w2(z),R2(x),R1(y)
S2:W1(x),R2(x),R2(z),W2(y),R1(z)
12/10/24
3-45
Purpose of Concurrency control
conflict.
12/10/24
3-46
Concurrency control
Techniques :
Lock based protocol
12/10/24
3-47
Locking
Locking is a mechanism that allows a transaction to reserve a data item
for its exclusive or shared use, and prevent other transactions from
modifying or reading.
Locking ensures the consistency and isolation of transactions.
A lock can be either exclusive or shared, depending on the type of
operation that the transaction performs on the data item.
An exclusive lock allows the transaction to read and write the data item,
and blocks other transactions from accessing it.
A shared lock allows the transaction to read the data item, and allows
other transactions to read it as well, but blocks any write operation.
12/10/24
3-48
Lock Manager
A Lock manager can be implemented as a separate process to
transactions
12/10/24
3-49
Types of lock
There are different types of locking in DBMS, depending on
the level of granularity, the duration, and the protocol of
the locks.
The level of granularity refers to the size of the data item
that the lock covers, such as a record, a page, or a table.
The duration refers to the time period that the lock is held
by the transaction, such as until the end of the transaction,
or until the end of the operation.
The protocol refers to the rules that the transaction follows
to acquire and release locks, such as two-phase locking,
timestamp ordering, or optimistic concurrency control.
12/10/24
3-50
Lock based protocol
transaction.
Locking is a procedure used
results
All data item must be accessed in mutually exclusive manner.
12/10/24
3-51
Lock Granularity
Indicates the level of lock use
Locking can take place at the following levels:
Database-level lock
Entire database is locked
Table-level lock
Entire table is locked
Page-level lock
Entire diskpage is locked
Row-level lock
Allows concurrent transactions to access different rows of the same
table, even if the rows are located on the same page
Field-level lock
Allows concurrent transactions to access the same row, as long as they
require the use of different fields (attributes) within that row
12/10/24
3-52
A Database-Level Locking Sequence
T1 and T2 can access the same database concurrently as long as they use different tables
Can cause bottlenecks when many transactions are trying to access the same table (even if the
transactions want to access different parts of the table and would not interfere with each other)
Not suitable for multi-user DBMSs
12/10/24
Page-Level Lock
An entire disk page is locked (a table can span several pages and
each page can contain several rows of one or more tables)
Most frequently used multi-user DBMS locking method
12/10/24
Row-Level Lock
12/10/24
Modes of locks
Data items can be locked in two modes :
Read Write
Read S X
Write X X
Or
S X
S True False
S False False
12/10/24
3-60
Example
Ex of lock protocol
T1 T2 Exclusive lock
Lock-X(B)
R(B)
B=B-50
W(B)
Unlock(B) Shared lock
Lock-S(B)
R(B)
Unlock(B)
W(B)
12/10/24
Unlock(B) 3-62
Conversion of locks
12/10/24
3-63
Conversion of locks
If we need to read to data item
x, it acquiring share lock similarly ,if the write is over I
want to perform read or some
lock-s(x) other trans read it would not be
Read(x) access because its exclusively
Meanwhile, if we want to write lock, so we downgrade the lock
operation on data item x so, by adding lock-s after write
before we perform write operation
operation we have to upgrade lock(lock-s).
this lock(lock-s). Lock-s(x)
Lock-s(x) Read(x)
Lock-x(x)
Read(x)
Write(x)
Lock-x(x) lock-s(x)
Write(x) Read(x)
12/10/24
3-64
Two phase Locking protocol
Defines how transactions acquire and relinquish
locks
Its requires both locks and unlocks being done in two
phases. (2PL)
2PL has two phases one is growing where all the
locks are being acquired by the transaction, and
second phase is shrinking ,where the locks held by
the transaction are being released.
Guarantees serializability, but it does not prevent
deadlocks
12/10/24
3-65
Two-Phase Locking
o Growing phase, in which a transaction acquires all the
required locks without unlocking any data
o Shrinking phase, in which a transaction releases all locks and
cannot obtain any new lock
12/10/24
3-66
Two-Phase Locking Protocol
12/10/24
Problem with 2phase Locking
2PL protocol enforces serializability but may reduce
concurrency due to the ff reasons
Holding Lock un-necessarily
Operation can wait even if the data whichis no longer
required for other transaction. So there is unnessarily
wait due to early lock
Deadlock
Cascading Rollback
To guarantee serializability, need an additional protocol
concerning the positioning of lock and unlock operations in
every transaction.
12/10/24
3-68
Problem. Unnecessarily…
Example
If T2 want to read a data item A
T1 T2 lock-s(A)It cant be data item A
Lock-x(A) Exclusively locked by T1 until all
Operation finish it has be wait,
R(A)
which Unnecessarily wait this
W(A) because of early lock
Lock-X(B)
Lock-s(A)
12/10/24
There is a cycle in the graph so its not conflict seriable. 3-69
Deadlock
A system is in a deadlock state if there exists a set
of transactions such that every transaction in the set
is waiting for another transaction in the set.
Assume both T1 and T2 need data items x and y to
complete the transaction.
T1 T2
Lock-x(X) T1 waiting to release lock y and
X=x+y T2 also waiting T1 to release lock
Lock-x(Y)
On x .this state is
Deadlock state.
Y-y-x
12/10/24
3-70
Time stamping
Each transaction is assigned a unique time-stamp.
A. Wait/die
Older transaction waits and the younger is rolled back and rescheduled
B. Wound/wait
Older transaction rolls back the younger transaction and reschedules it
In the situation where a transaction is requests multiple locks,
12/10/24
Problem. Cascading
rollback…
Example
First T1 acquire lock and r and w
T1 T2 then unlock. What ever Write byT1
Lx(A) on A read By T2 for data item A,
Similar to T2 and T3.
R(A)
But T1 not yet committed T2 read
W(A) Dirty data if T1 fails some where
u(A) T2 roll back and T3 also rollback
XL(A)
R(A)
W(A)
U(A)
XL(A)
12/10/24
There …..
is a cycle in the graph so its not conflict seriable. 3-73
Possible solution for problem in 2PL
Conservative 2-phase lock protocol
Lock all that you need before transaction start
This means you have to do predicted how many data item
are used before you start.
If T1 has used data item A,B,C,D ,it request lock on all
then if all data item are guaranteed it start if it waits until
guaranteed ,
We don’t have growing phase. It have only shrinking.
Advantage : No deadlock situation because all acquired
before start
Disadvantage : practical implementation is difficult
12/10/24
3-74
Possible solution for problem in 2PL
Strict 2-phase lock protocol
Transaction T does not release any of exclusive lock until
after it commits or abort.
Most popular one because it is recover easily.
)
C
l(
U
)R
l(A
B
l(
)W ).
A .U
l( L(
C)
R
End Tx
StartTx Commit
..
But there is deadlock. UL(B)
12/10/24
3-75
Strict 2-phase
What kind of locking the following schedule?
T1
Lock-s(A)
Read(A)
Lock-x(B)
Read(B)
Write(B)
commit
Unlock(B)
Unlock(A)
12/10/24
3-76
Deadlock
Control techniques
Deadlock prevention
Deadlock detection
Deadlock avoidance
environment
12/10/24
3-77
Deadlock Prevention
We can prevent deadlocks by giving each transaction a priority
and ensuring that lower priority transactions are not allowed to
wait for higher priority transactions
One way to assign priorities is to give each transaction a
timestamp when it starts up.
The lower the timestamp, the higher the transaction's priority,
that is, the oldest transaction has the highest priority.
Assume Ti requests a lock, but Tj holds a conflicting lock. We
can follow two strategies:
Wait-die: if Ti has higher priority, it waits; else Ti aborts.
Wound-wait: if Ti has higher priority, abort Tj; else Ti waits.
Note: after aborting, restart with original timestamp!
12/10/24
3-78
Deadlock Detection
DBMS must periodically check for deadlocks.
12/10/24
3-81
Transaction Failure …
What are the transaction failure?
Disk Failure –loss of data in disk blocks during a
transaction. i.e. the system cant read/write the disk
properly
Physical problem and catastrophes – problems s like
power failure,
12/10/24
3-82
Scenarios ….
System crashes in the middle of a transaction T;
12/10/24
3-83
Logging
Log is Sequence of log records, recording all changes made
to the database
Written to stable storage (e.g.,disk) during normal operation
Used in recovery
12/10/24
3-84
Transaction Failure and
recovery and log
When the transaction submitted to database
system. In this the DBMS responsible to excused
ALL of operation because in any case the DBMS
should not allow to half execute and half of
bypass.
When the transaction was running its operation
some system failed occurred.
In this case we should have a mechanism to do
recovery and this the help of log file.
12/10/24
3-85
Recovery Algorithms
Recovery algorithms are techniques to ensure database
Transaction ID
[ Start_transaction,Tid]
[write-item.Tid, X,old-value, new-value]
[read-item,Tid,X]
[commit,Tid] [Abort,Tid]
12/10/24
3-88