0% found this document useful (0 votes)
48 views36 pages

Lec 8 - IDB

This document contains lecture slides on transaction management and concurrency control from a course on database systems at Balochistan University of Information Technology, Engineering & Management Sciences. The slides define what a transaction is, explain why transactions are needed to deal with failures and concurrent execution, describe the ACID properties of atomicity, consistency, isolation and durability, and discuss transaction states.

Uploaded by

Zahoor ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views36 pages

Lec 8 - IDB

This document contains lecture slides on transaction management and concurrency control from a course on database systems at Balochistan University of Information Technology, Engineering & Management Sciences. The slides define what a transaction is, explain why transactions are needed to deal with failures and concurrent execution, describe the ACID properties of atomicity, consistency, isolation and durability, and discuss transaction states.

Uploaded by

Zahoor ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Balochistan University of Information Technology, Engineering & Management Sciences

Introduction to
Database Systems
Mohammad Imran
Lecturer
Department of Information Technology
Balochistan University of Information Technology, Engineering & Management Sciences

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015
Balochistan University of Information Technology, Engineering & Management Sciences

Lecture 8

Transactions
Transaction Management & Concurrency Control

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 2
Balochistan University of Information Technology, Engineering & Management Sciences

So far till now


• Introduction to Database System
• Basic Database Concepts
o Conceptual, Logical & Physical Modeling
• Logical database Modelling and design
o Entity Relationship diagram (ERD)
o Enhanced ERD
• Relational data model:
o mapping ERD to relational model
• Relational Algebra
• Functional dependencies
o Normalization (1st - 3rd Normal Form)
• Structured Query language (SQL) (covered in Labs)
• Basics about database connectivity with front end, ASP.NET (covered in last lab)

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 3
Balochistan University of Information Technology, Engineering & Management Sciences

What is a Transaction?
• A transaction is a sequence of operations on database that
is treated as a single logical operation (abbreviation = txn)
• E.g. transaction to transfer $50 from account A to account B:
1. read(A)
2. A := A – 50
3. write(A)
4. read(B)
5. B := B + 50
6. write(B)
Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 4
Balochistan University of Information Technology, Engineering & Management Sciences

Why Transactions?
• Two main issues to deal with:
1. Failures of various kinds, such as hardware failures and system
crashes
2. Concurrent execution of multiple transactions
• Take an example of software/ hardware failure (reason 1):
1. read balance1
2. write(balance1 - 500)
CRASH
3. read balance2
4. write(balance2 + 500)

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 5
Balochistan University of Information Technology, Engineering & Management Sciences

Why Transactions? (Contd.)


• Take an example of issues may be faced due to
concurrent execution (reason 2):
User 1 User 2
read balance1
write(balance1 – 500)
read balance1
read balance2
if (balance1 + balance2 < min)
write(balance1 – fee)
read balance2
write(balance2 + 500)

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 6
Balochistan University of Information Technology, Engineering & Management Sciences

ACID Properties
• To preserve the integrity of data the database system must ensure, ACID
• Obviously its not ACID like H2SO4 (Sulphuric Acid) or HCL (Hydrochloric
Acid)
• ACID properties are:
o Atomicity
o Consistency
o Isolation
o Durability
• Lets see an example and understand ACID properties in parallel
Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 7
Balochistan University of Information Technology, Engineering & Management Sciences

Example of Fund Transfer


1. read(A)
2. A := A – 50
3. write(A)
4. read(B)
5. B := B + 50
6. write(B)

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 8
Balochistan University of Information Technology, Engineering & Management Sciences

Atomicity Requirement
• If the transaction fails after step
3 and before step 6, money will
1. read(A)
be “lost” leading to an
inconsistent database state 2. A := A – 50
o Failure could be due to software 3. write(A)
or hardware
4. read(B)
• System should ensure that
updates of a partially 5. B := B + 50
executed transaction are not 6. write(B)
reflected in the database

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 9
Balochistan University of Information Technology, Engineering & Management Sciences

Consistency Requirement
• Sum of A and B is unchanged by
the execution of the transaction o read(A)
• In general, consistency o A := A – 50
requirements include
o write(A)
o Explicitly specified integrity
constraints such as primary keys o read(B)
and foreign keys o B := B + 50
o Implicit integrity constraints o write(B)
• e.g. sum of balances of all accounts,
minus sum of loan amounts must
equal value of cash-in-hand

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 10
Balochistan University of Information Technology, Engineering & Management Sciences

Consistency Requirement (Contd.)


• A transaction must see a
consistent database o read(A)
• During transaction execution o A := A – 50
the database may be
o write(A)
temporarily inconsistent
• When the transaction o read(B)
completes successfully the o B := B + 50
database must be consistent o write(B)
o Erroneous transaction logic
can lead to inconsistency

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 11
Balochistan University of Information Technology, Engineering & Management Sciences

Isolation Requirement
• If between steps 3 and 6, another
T1 T2
transaction T2 is allowed to access the
partially updated database, It will see an 1. read(A)
inconsistent database i.e. the sum A+B will 2. A := A – 50
be less than it should be 3. write(A)
• Isolation can be ensured trivially by running read(A),
read(B),
transactions serially (one after the other)
print(A+B)
• However, executing multiple transactions
4. read(B)
concurrently has significant benefits
5. B := B + 50
• Question: How executing multiple 6. write(B)
transactions make sense?
Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 12
Balochistan University of Information Technology, Engineering & Management Sciences

Durability requirement
• Once the user has been notified
that the transaction has completed 1. read(A)
(i.e., the transfer of the $50 has 2. A := A – 50
taken place), the updates to the 3. write(A)
database by the transaction must 4. read(B)
persist even if there are software or 5. B := B + 50
hardware failures 6. write(B)

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 13
Balochistan University of Information Technology, Engineering & Management Sciences

ACID Properties at a Glance


• Atomicity
o Either all operations of the transaction are properly reflected in the database or none
are
• Consistency
o Transaction take the database from one consistent state to another
o Consistent: satisfies the constraints from the schema, and any other expectations
about the values in the database
• Isolation
o Transaction is not affected by and does not affect other concurrent transactions
• Durability
o After a transaction completes successfully, the changes it has made to the database
persist, even if there are system failures

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 14
Balochistan University of Information Technology, Engineering & Management Sciences

Transaction States
• Active: the initial state; the transaction stays in this state while it is executing
• Partially committed: after the final statement has been executed
• Failed: after the discovery that normal execution can no longer proceed
• Aborted: after the transaction has been rolled back and the database restored to
its state prior to the start of the transaction. Two options after it has been aborted:
o restart the transaction
• can be done only if no internal logical error
o kill the transaction
• Committed: after successful completion

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 15
Balochistan University of Information Technology, Engineering & Management Sciences

Transaction States (Contd.)

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 16
Balochistan University of Information Technology, Engineering & Management Sciences

Concurrent Executions
• Multiple transactions are allowed to run concurrently in
the system
• Advantages are:
o increased processor and disk utilization, leading to better
transaction throughput
• E.g. one transaction can be using the CPU while another is
reading from or writing to the disk
o reduced average response time for transactions: short
transactions need not wait behind long ones
Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 17
Balochistan University of Information Technology, Engineering & Management Sciences

Concurrency Control Schemes


• The mechanisms to achieve isolation
o To control the interaction among the concurrent
transactions in order to prevent them from destroying the
consistency of the database

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 18
Balochistan University of Information Technology, Engineering & Management Sciences

Schedules
• Schedule: a sequences of instructions that specify the
chronological order in which instructions of concurrent
transactions are executed
o A schedule for a set of transactions must consist of all
instructions of those transactions
o Must preserve the order in which the instructions appear in
each individual transaction

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 19
Balochistan University of Information Technology, Engineering & Management Sciences

Schedules (Contd.)
• A transaction that successfully completes its execution
will have a commit instructions as the last statement
o by default transaction assumed to execute commit
instruction as its last step
• A transaction that fails to successfully complete its
execution will have an abort instruction as the last
statement

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 20
Balochistan University of Information Technology, Engineering & Management Sciences

Schedule 1
• Let T1 transfer $50 from
A to B, and T2 transfer
10% of the balance from
A to B
• A serial schedule in
which T1 is followed by
T2

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 21
Balochistan University of Information Technology, Engineering & Management Sciences

Schedule 2
• A serial schedule where
T2 is followed by T1

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 22
Balochistan University of Information Technology, Engineering & Management Sciences

Schedule 3
• The following schedule is
not a serial schedule, but it
is equivalent to Schedule
1
• In Schedules 1, 2 and 3,
the sum A + B is preserved

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 23
Balochistan University of Information Technology, Engineering & Management Sciences

Schedule 4
• The following concurrent
schedule does not
preserve the value of
(A + B )

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 24
Balochistan University of Information Technology, Engineering & Management Sciences

Serializability
• 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

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 25
Balochistan University of Information Technology, Engineering & Management Sciences

Simplified view of transactions


o We ignore operations other than read and write
instructions
o We assume that transactions may perform arbitrary
computations on data in local buffers in between reads
and writes
o Our simplified schedules consist of only read and write
instructions

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 26
Balochistan University of Information Technology, Engineering & Management Sciences

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
wrote 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
• Intuitively, a conflict between li and lj forces a (logical) temporal order between
them
o If li and lj are consecutive in a schedule and they do not conflict, their results
would remain the same even if they had been interchanged in the schedule

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 27
Balochistan University of Information Technology, Engineering & Management Sciences

Conflict Serializability
• Rather than ensuring Serializability, it’s easier to ensure a stricter condition
known as conflict Serializability.

• A conflict in a schedule is a pair of actions (reads or writes) that cannot be


swapped without potentially changing the behavior of one or more of the
transactions in the schedule.
T1 T2
• example: in the schedule at right, T1's write of A and
r(A)
T2's read of A conflict. why? r(B)
w(A)
• A schedule is conflict serializable if we can turn r(A)
it into an equivalent serial schedule by swapping
w(A)
pairs of consecutive actions that don’t conflict.

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 28
Balochistan University of Information Technology, Engineering & Management Sciences

Which Actions Conflict?


• Actions in different transactions conflict if:
1) they involve the same data item
2) at least one of them is a write

• Pairs of actions that do conflict:


• wi(A); rj(A) the value read by Tj may change if we swap them
• ri(A); wj(A) the value read by Ti may change if we swap them
• wi(A); wj(A) subsequent reads may change if we swap them
• two actions from the same txn (their order is fixed by the client)

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 29
Balochistan University of Information Technology, Engineering & Management Sciences

Pairs of actions that don’t conflict


• ri(A); rj(A)
• ri(A); rj(B)
• ri(A); wj(B)
• wi(A); rj(B)
• wi(A); wj(B)

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 30
Balochistan University of Information Technology, Engineering & Management Sciences

Example: Conflict Serializable Schedule


• r2(A); r1(A); r2(B); w1(A); w2(B); r1(B); w1(B) T1 T2 T1 T2
r(A) r(A) r(A)
r(B)
• r2(A); r2(B); r1(A); w1(A); w2(B); r1(B); w1(B) r(B) w(B)
w(A) r(A)
w(B) w(A)
• r2(A); r2(B); r1(A); w2(B); w1(A); r1(B); w1(B) r(B) r(B)
w(B) w(B)

• r2(A); r2(B); w2(B); r1(A); w1(A); r1(B); w1(B)

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 31
Balochistan University of Information Technology, Engineering & Management Sciences

Testing for Conflict Serializability


• Because conflicting pairs of actions can't be swapped, they impose
constraints on the order of the transactions in an equivalent serial schedule
• Example: if a schedule includes w1(A) … r2(A), T1 must come before T2 in
any equivalent serial schedule
• To see if we can create an equivalent serial schedule, determine all such
constraints and make sure they aren’t contradictory.
• Example: r2(A); r1(A); r2(B); w1(A); w2(B); r1(B); w1(B)
• r2(A) … w1(A) means T2 must come before T1
no contradictions, so this
• r2(B) … w1(B) means T2 must come before T1 schedule is equivalent to
• w2(B) … r1(B) means T2 must come before T1 the serial ordering T2;T1
• w2(B) … w1(B) means T2 must come before T1
• Thus, this schedule is conflict serializable.
Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 32
Balochistan University of Information Technology, Engineering & Management Sciences

Testing for Conflict Serializability (Contd.)


• What about this schedule? r1(B); w1(B); r2(B); r2(A); w2(A); r1(A)

• w1(B) … r2(B) means T1 must come before T2


contradiction!

• w2(A) … r1(A) means T2 must come before T1

• Thus, this schedule is not conflict serializable

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 33
Balochistan University of Information Technology, Engineering & Management Sciences

Testing for Conflict Serializability (Contd.)


• The algorithm for this test makes use of a precedence graph
• the vertices are the transactions
• add an edge for each precedence constraint: T1  T2 means T1 must come before
T2 in an equivalent serial schedule
• Example: r2(A); r3(A); r1(B); w4(A); w2(B); r3(B) T1 T2
r2(A) … w4(A) means T2  T4
r3(A) … w4(A) means T3  T4
r1(B) … w2(B) means T1  T2 T3 T4
w2(B) … r3(B) means T2  T3
• Algorithm:
• construct the precedence graph
• test for cycles (i.e., paths of the form A  …  A)
• if the graph is acyclic (no cycles), the schedule is conflict serializable
Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 34
Balochistan University of Information Technology, Engineering & Management Sciences

Exercise
• Determine if the following are conflict serializable:
• r1(A); r3(A); w1(A); w2(A); r2(B); w3(B); w4(B) T1 T2

T3 T4

T1 T2

• r1(A); w3(A); w4(A); w2(B); r2(B); r1(B); r4(B)


T3 T4

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 35
Balochistan University of Information Technology, Engineering & Management Sciences

Thank you

Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015

You might also like