Lecture 24 - 26-Transaction Management
Lecture 24 - 26-Transaction Management
Database Systems
Transaction Management &
Concurrency Control
3
Database Usage
▪ You are using multiple databases
Everyday knowingly or Unknowingly
Examples Uses
▪ Using ATM
▪ Using your Campus Learning
Management System
▪ Using Facebook
Database Usage
Database Usage
What is a Transaction?
10
Example Transaction 1
▪ Register credit sale of 100 units of product
X to customer Y for $500
▪ Reduce product X’s quantity by 100
▪ Adding $500 to customer Y’s account
receivable
UPDATE PRODUCT
SET PROD_QOH = PROD_QOH - 100
WHERE PROD_CODE = ‘X’;
UPDATE ACCT_RECEIVABLE
SET ACCT_AMOUNT = ACCT_ AMOUNT + 500
11
WHERE ACCT_NUM = ‘Y’;
Transaction
12
Transaction States
13
Properties of Transaction
▪ Atomicity
▪ Consistency
▪ Isolation
▪ Durability
15
Transaction Properties
16
Transaction Properties
▪ Before a transaction starts and after a transaction is completed, sum of money should be same.
18
Transaction Properties
19
20
Isolation Example
21
Transaction Properties
▪ Serial Scheduling
▪ No other transaction can interfere unless the current transaction is executed.
▪ Maintains consistency
▪ Problem: waiting time, other transactions will have to wait
▪ Performance is degraded in terms of throughput: num of transactions
executed/time
▪ Parallel Scheduling
▪ Multiple transactions can be executed
▪ CPU is switched to enable concurrent transparency
▪ Read-write, write-read, write-write conflicts
23
Concurrency Transparency
25
Concurrency Control
▪ Coordinates simultaneous transaction execution in
multiprocessing database.
27
Unrepeatable Read Problem
T1 T2 ▪ When you read a variable multiple times and
R(A) each time you get a different value.
R=10
▪ T2 read A as 10, meanwhile T1 writes a
R(A)
R=10
different value on A (11).
W(A) ▪ If T2 reads A again, it gets to read a different
R=11 value (11), although it didn’t write on A.
…. R(A) ▪ T2 was thinking that it is performing in
R=11
isolation, however T1 changed A value.
….
▪ A read that cannot be repeated.
▪ Inconsistent retrievals
28
Phantom Read Problem
T1 T2 ▪ T2 reads A for the first time.
R(A) ▪ T1 deletes A.
R(A) ▪ T2 reads A for the second time and gets an
del A error because A doesn’t exist.
…. R(A) ▪ When you aren’t able to read a variable for
the second time due to change in the
structure of the data items, that is the
phantom read problem.
29
Lost Update
(write-write)Problem
T1 T2 ▪ T2 doesn’t read A, just writes on it. This is
R(A) referred to as blind write.
A=10 ▪ T1 reads A as 10.
W(A)
A=11 ▪ Writes 11 on it.
W(A) ▪ T2 performs a blind write and commits.
A=50
▪ T1 wrote a different value of A: 11, however
C
this value is being overwritten by T2, but T1
C
isn’t aware of it.
▪ 11 is being lost and updated to 50, but T1
doesn’t know this.
30
The Scheduler
▪ How is the correct order? Who determines
the order?
▪ Fortunately, DBMS handles that tricky
assignment for us by using a built-in
scheduler
▪ Scheduler establishes order of concurrent
transaction execution
38
The Scheduler
▪ Ensures execution of database operations to ensure
serializability
▪ Bases actions on concurrency control algorithms
▪ Locking
▪ Time stamping
▪ Ensures efficient use of computer’s CPU
▪ No scheduling, make transaction first comes first serve
(FCFS)
▪ Lost CPU cycles during processing of I/O
▪ If FCFS, one transaction will be performed at a time, while its I/O
operations CPU cycles will be wasted 39
Read/Write Conflict Scenarios:
Conflicting Database Operations
Matrix
Table 9.9
40
Concurrency Control
with Locking Methods
▪ Lock guarantees current transaction
exclusive use of data item (T2 does not
have to access to the data item that
currently being used by transaction t1)
▪ Acquires lock prior to access
▪ Lock is released (unlocked) when
transaction is completed. So other
transactions can lock the data
41
Concurrency Control
with Locking Methods
▪ DBMS automatically initiates and enforces
locking procedures
▪ Managed by lock manager (built-in DBMS),
which is responsible for assigning and
policing the locks used by the transactions
▪ Lock granularity indicates level of lock use
42
Lock Granularity
▪ Lock granularity indicates level of lock use
▪ Locking can take place at the following
levels:
▪ Database
▪ Table
▪ Page
▪ Row
▪ Even field (attribute)
43
Database-Level Locking Sequence
• A complete entity database is locked
• Preventing the use of any tables in the
database by transaction T2 while
transaction t1 is being executed.
• Good for batch processes
• Unsuitable for online multi-user DBMSs (it
waits for previous transaction to be
completed before execution of next
transaction)
44
Database-Level Locking Sequence
Figure 9.2
45
Table-Level Locking Sequence
▪ A entire table is locked
▪ Preventing access to any row by
transaction T2 while transaction t1 is using
the table.
▪ Less restrictive than database locks
▪ Not suitable for multi-user DBMSs
46
Table-Level Lock Example
Figure 9.3
47
Page-Level Locking Sequence
• Lock an entire diskpage, page
(equivalent of a disk lock), described
as a referenced section of a disk.
• A table can span several pages
• Each page contains several rows of
one or more tables
• Page-level locks are currently the most
frequently used multi-user DBMSs
locking method 48
Page-Level Lock Example
Figure 9.4
50
Row-Level Lock Example
Figure 9.5
51
Field (attribute)-Level Locking
Sequence
▪ Allows concurrent transactions to access
the same row, as long as they require the
use of different fields (attributes) within
the row.
▪ Most flexible multi-data access, it is rarely
done because it requires an extremely
high level of computer overhead.
52
Lock Types: Binary Locks and
Shared/Exclusive Locks
▪ Binary locks
▪ Two states
▪ Locked (1)
▪ Unlocked (0)
▪ Transaction must unlock the object after its
termination
▪ Automatically managed or scheduled by DBMSs
▪ User does not need to concerned about locking
or unlocking data items
53
Shared/Exclusive Locks
▪ Exclusive
▪ Used when potential for conflict exists.
▪ Issued when transaction wants to update
unlocked data.
54
Shared/Exclusive Locks
▪ Shared
▪ Exists when concurrent transactions granted
READ access
▪ Produces no conflict for read-only transactions
▪ Issued when transaction wants to read and
Exclusive Lock not held on item
55
Deadlocks
▪ Occurs when two transactions wait for
each other to unlock data
56
Deadlocks
▪ A deadlock occurs when two transactions have a
lock, each on a separate object, and they want
to acquire a lock on each other's object.
▪ A deadlock is a condition where two or more
transactions are waiting indefinitely for one
another to give up locks.
▪ Deadlock is said to be one of the most feared
complications in DBMS as no task ever gets
finished and is in waiting state forever.
57
In the student table, transaction T1 holds a lock on some Student rows and needs
to update some rows in the grade table. Simultaneously, transaction T2 holds locks
on some rows in the grade table and needs to update the rows in the Student table
held by Transaction T1.
Now Transaction T1 is waiting for T2 to release its lock and similarly, transaction T2
is waiting for T1 to release its lock. All activities come to a halt state and remain at a
standstill. It will remain in a standstill until the DBMS detects the deadlock and
aborts one of the transactions.
58
Deadlock Prevention
▪ For a large database, the deadlock prevention
method is suitable.
▪ A deadlock can be prevented if the resources are
allocated in such a way that deadlock never
occurs.
▪ The DBMS analyzes the operations whether they
can create a deadlock situation or not, If they do,
that transaction is never allowed to be executed.
59
Deadlocks
▪ In such a case, SQL detects the deadlock
automatically and solves the problem by
aborting one of the two transactions.
Prevention Methods
• Mutual Exclusion: that prevents simultaneous access to a shared
resource.
• Hold and Wait: infinite loop
• No preemption: Not temporarily interrupting an executing task
• Circular wait: Two or more processes wait for resources in a circular
order. To eliminate circular wait, we assign a priority to each
resource. 60
Serializability
▪ Serializability is a concept in database systems
that ensures transactions are executed in a
manner that preserves consistency and
correctness of the database state, despite their
concurrent execution.
▪ It guarantees that the final outcome of executing
multiple transactions concurrently is equivalent to
the outcome that would be obtained if the
transactions were executed in some serial order,
one after the other.
61
Serializability
T1 T2
R(a)
R(a) T1 T2
W(a)
W(a) When you get a loop, it
means it’s a parallel
schedule.
▪ Is it serializable?
▪ It means find a clone of this parallel schedule that is a serial scheduler.
▪ To convert it we can either make it T1 → T2 or T2→T1
▪ Two methods to check if its serializable.
▪ Conflict Equivalent serializable
62
▪ View serializable
Conflict Serializability
▪ Conflict serializability is based on the notion of
conflicting operations between transactions.
▪ A schedule (or execution order) of transactions is
conflict serializable if it can be transformed into a
serial schedule by swapping non-conflicting
operations while preserving the order of conflicting
operations.
▪ Conflicting operations include read-write, write-read,
and write-write operations on the same data item by
different transactions.
63
Conflict Equivalent
▪ Is S equivalent to S^
▪ Check for adjacent non-conflicting pairs
S S^
T1 T2 T1 T2
R(a) R(a)
W(a) W(a)
R(a) R(b)
W(a) R(a)
R(b) W(a)
64
Conflict Equivalent
▪ Is S=S^
▪ Check for adjacent non-conflicting pairs
S S^
T1 T2 T1 T2
R(a) R(a)
W(a) W(a)
R(a) R(b)
W(a) R(a)
R(b) W(a) 65
Conflict Equivalent
▪ Is S=S^
▪ Check for adjacent non-conflicting pairs
S S^
T1 T2 T1 T2
R(a) R(a)
W(a) W(a)
R(a) R(b)
R(b) R(a)
W(a) W(a) 66
Conflict Equivalent
▪ Is S=S^
▪ Check for adjacent non-conflicting pairs
S S^
T1 T2 T1 T2
R(a) R(a)
W(a) W(a)
R(a) R(b)
R(b) R(a)
W(a) W(a) 67
Conflict Equivalent
▪ Is S=S^
▪ Check for adjacent non-conflicting pairs
S S^
T1 T2 T1 T2
R(a) R(a)
W(a) W(a)
R(b) R(b)
R(a) R(a)
W(a) W(a) 68
Conflict Equivalent
▪ If for S scheduler there is a conflict equivalent S^
then it means S is serializable
▪ S→S^→ serializable
S S^
T1 T2 T1 T2
R(a) R(a)
W(a) W(a)
R(b) R(b)
R(a) R(a)
W(a) W(a) 69
Conflict Equivalent
T1 T2
▪ Another example R(a)
▪ Which are adjacent pairs here? W(a)
R(a)
W(a)
R(b)
70
Conflict Equivalent
▪ Another example
▪ Which are adjacent pairs here?
▪ Are these non-conflicting?
▪ No, hence no change in positions
T1 T2
R(a)
W(a)
R(a)
W(a)
R(b) 71
Example 2: Conflict Equivalent
Serializable
Check conflict pairs in other transactions
and draw edge.
T1 T2 T3
Make Precedence Graph
R(x)
R(y)
Check for conflicting pairs.
R(x)
R(y)
Conflict of R(X) is W(X): so find W(X)
R(z)
in T2 and T3.
W(y)
W(Z) T1
T2
R(Z)
W(X)
W(Z) T3
10
Conflict Equivalent Serializable
Check conflict pairs in other
T1 T2 T3
transactions and draw edge.
R(x)
R(y)
R(x)
For W(Y): u’ll check for 2 conflicts
R(y)
R(Y), W(Y)
R(z)
W(y) T1 T2
W(Z)
R(Z)
W(X)
T3
W(Z) 73
Conflict Equivalent Serializable
Check conflict pairs in other
T1 T2 T3
transactions and draw edge.
R(x)
Does the graph have a loop/cycle?
R(y)
No, means its
R(x)
R(y)
R(z)
Conflict T1 T2
W(y)
Serializable
W(Z)
R(Z)
W(X)
T3
W(Z) 74
Conflict Equivalent Serializable
If a schedule is conflict serializable, it means there will be an
equivalent schedule that is serialized.
So it is consistent.
T3
75
Conflict Equivalent Serializable
What combinations will be formed? What sequence of
transactions will be formed?
From the graph check which in-degree of the vertex is
zero.
T2 (no arrow towards T2) T1 T2
T3
76
Conflict Equivalent Serializable
If a schedule is conflict serializable, it means there will be an
equivalent schedule that is serialized.
So it is consistent.
What combinations will be formed? Which sequence will be
formed?
From the graph check which T1
in-degree of the vertex is zero.
T2
T3 T3
77
Conflict Equivalent Serializable
If a schedule is conflict serializable, it means there will be an
equivalent schedule that is serialized.
So it is consistent.
What combinations will be formed? Which sequence will be
formed?
From the graph check which T1
in-degree of the vertex is zero.
T2
T3
T1
78
▪ Que:Check serializability for this schedule.
T1 T2 T3
T1 T2
R(A)
W(A) Get a loop so wont
W(A) T3 draw further
W(A)
This schedule is not conflict
equivalent but is it serializable?
▪ We will use view serializable.
79
View Serializability
▪ View serializability is based on the idea of the
database state seen by transactions.
▪ A schedule is view serializable if it produces
the same set of results (or views) as some
serial schedule, regardless of the specific
operations performed by transactions.
▪ View serializability is more general than
conflict serializability and considers the overall
effect of transactions on the database state
rather than focusing on specific conflicting
operations.
▪ Que:Check serializability for this schedule.
T1 T2 T3
T1 T2
R(A)
W(A) Get a loop so wont
W(A) T3 draw further
W(A)
This schedule is not conflict
equivalent but is it serializable?
▪ We will use view serializable.
Test for values of A in
T1 T2 T3
both tables.
R(A) A=100
W(A) A-40
W(A) A-40
W(A) A-20 81
▪ Check serializability for this schedule.
T1 T2 T3
T1 T2
R(A)
W(A) Get a loop so wont
W(A) T3 draw further
W(A)
This schedule is not conflict
equivalent but is it serializable?
▪ We will use view serializable.
Test for values of A in
T1 T2 T3
both tables.
R(A) A=100
The answers are same
W(A) A-40
hence both schedulers
W(A) A-40
are equivalent.
W(A) A-20 82
View Serializable
▪ Rules
▪ For each data item in the transaction,
check these three rules:
1. Initial Read
2. Update
3. Final Update
83
Example 1: View Serializable
▪ Check can this be solved through
T1 T2 T3 conflict equivalent method?
R(B)
W(A) ▪ Do you get a loop while checking
for conflicting pairs?
R(A)
R(A)
W(B)
W(B)
W(B)
84
Example 1: View Serializable
T1 T2 T3
R(B)
A B
W(A)
R(A) Initial Read
R(A) Update
85
Example 1: View Serializable
T1 T2 T3
R(B)
A B
W(A)
R(A) Initial Read
R(A) Update
▪ To find the sequence of transactions, we see that our initial read for
B is T2 and final update is T3, so sequence becomes
▪ T2 → T1→ T3
23
▪ You can check by assigning values to A and B.
Example 2: View Serializable
T1 T2 T3
R(Z)
R(Y)
W(Y) X Y Z
R(Y) Initial T1 T2 T2
R(Z) Read
R(X) Update T1, T2 T2,T3,T1 T2
W(X) Final T2 T1 T2
Update
W(Y)
W(Z)
R(Z)
R(Y)
W(Y)
W(X) 24
Example 2: View Serializable
T1 T2 T3
X Y Z
R(Z)
Initial T1 T2 T2
R(Y)
Read
W(Y)
Update T1, T2 T2,T3,T1 T2
R(Y)
Final T2 T1 T2
R(Z) Update
R(X)
W(X) Now, we must find a sequence of transactions.
W(Y) Since Z has T2 only in all columns, this can be
ignored.
W(Z)
See for X, Y
R(Z)
X: T1→T2, Y: T2→ T3→T1
R(Y)
X and Y, both contradict each other.
W(Y)
Hence No order consists for this schedule.
W(X)
So, its not view serializable. 25
Practice Ques:
T1 T2 T3 Find if this schedule is
R(A) serializable.
W(A)
R(A) Make precedence graph.
W(A) If you find loop, then check
W(A) with view serializable method.
90
Practice Ques:
Make precedence graph, it
T1 T2 T3 has loops
R(A)
W(A) Make the table for view
R(A) serializable and find the
W(A) sequence.
W(A)
A
Initial Read T1
T1 T2 Update T2, T1, T3
Final Update T3
T3 Answer: T1 → T2 → T3 91