Lec 8 - IDB
Lec 8 - IDB
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
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
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
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
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
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
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
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
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.
Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 28
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 29
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 30
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 31
Balochistan University of Information Technology, Engineering & Management Sciences
Introduction to Database Systems Spring 2015 Mohammad Imran July 13, 2015 33
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
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