Module 17: Transactions: Database System Concepts, 7 Ed
Module 17: Transactions: Database System Concepts, 7 Ed
Transaction Concept
Transaction State
Concurrent Executions
Serializability
Recoverability
Implementation of Isolation
Transaction Definition in SQL
Testing for Serializability.
Database System Concepts - 7th Edition 17.2 ©Silberschatz, Korth and Sudarshan
Transaction Concept
Database System Concepts - 7th Edition 17.3 ©Silberschatz, Korth and Sudarshan
Example of Fund Transfer
Database System Concepts - 7th Edition 17.4 ©Silberschatz, Korth and Sudarshan
Example of Fund Transfer (Cont.)
Database System Concepts - 7th Edition 17.5 ©Silberschatz, Korth and Sudarshan
Example of Fund Transfer (Cont.)
T1 T2
1. read(A)
2. A := A – 50
3. write(A)
read(A), read(B), print(A+B)
4. read(B)
5. B := B + 50
6. write(B
Isolation can be ensured trivially by running transactions
serially
• That is, one after the other.
However, executing multiple transactions concurrently has
significant benefits, as we will see later.
Database System Concepts - 7th Edition 17.6 ©Silberschatz, Korth and Sudarshan
ACID Properties
A transaction is a unit of program execution that accesses
and possibly updates various data items. To preserve the
integrity of data the database system must ensure:
Atomicity. Either all operations of the transaction are
properly reflected in the database or none are.
Consistency. Execution of a transaction in isolation
preserves the consistency of the database.
Isolation. Although multiple transactions may execute
concurrently, each transaction must be unaware of other
concurrently executing transactions. Intermediate
transaction results must be hidden from other concurrently
executed transactions.
• That is, for every pair of transactions Ti and Tj, it appears
to Ti that either Tj, finished execution before Ti started, or
Tj started execution after Ti finished.
Durability. After a transaction completes successfully, the
changes it has made to the database persist, even if there
are system failures.
Database System Concepts - 7th Edition 17.7 ©Silberschatz, Korth and Sudarshan
Transaction State
Database System Concepts - 7th Edition 17.8 ©Silberschatz, Korth and Sudarshan
Transaction State (Cont.)
Database System Concepts - 7th Edition 17.9 ©Silberschatz, Korth and Sudarshan
Concurrent Executions
Database System Concepts - 7th Edition 17.10 ©Silberschatz, Korth and Sudarshan
Schedules
Database System Concepts - 7th Edition 17.11 ©Silberschatz, Korth and Sudarshan
Schedule 1
Database System Concepts - 7th Edition 17.12 ©Silberschatz, Korth and Sudarshan
Schedule 2
Database System Concepts - 7th Edition 17.13 ©Silberschatz, Korth and Sudarshan
Schedule 3
Database System Concepts - 7th Edition 17.14 ©Silberschatz, Korth and Sudarshan
Schedule 4
Database System Concepts - 7th Edition 17.15 ©Silberschatz, Korth and Sudarshan
serializability is a property of a system describing how different processes
operate on shared data
Database System Concepts - 7th Edition 17.16 ©Silberschatz, Korth and Sudarshan
Transaction-1 Transaction-2
R(a)
W(a)
R(b)
W(b)
R(b)
R(a)
W(b)
W(a)
Transaction-2 begins its execution before Transaction-1 is finished, and they are both
working on the same data, i.e., “a” and “b”, interchangeably. Where “R”-Read, “W”-
Write
Database System Concepts - 7th Edition 17.17 ©Silberschatz, Korth and Sudarshan
Serializability
Database System Concepts - 7th Edition 17.18 ©Silberschatz, Korth and Sudarshan
Simplified view of transactions
We ignore operations other than read and write instructions
We assume that transactions may perform arbitrary
computations on data in local buffers in between reads and
writes.
Our simplified schedules consist of only read and write
instructions.
Database System Concepts - 7th Edition 17.19 ©Silberschatz, Korth and Sudarshan
Conflicting Instructions
Database System Concepts - 7th Edition 17.20 ©Silberschatz, Korth and Sudarshan
Conflict serializability is a type of conflict operation in serializability that operates the
same data item that should be executed in a particular order and maintains the consistency
of the database.
Tran Tran
Trans
sact sact
actio
ion – ion – It is a conflict serializable schedule as well as a serial schedule because the
n–1 graph (a DAG) has no loops. We can also determine the order of
2 3
(t1) transactions because it is a serial schedule.
(t2) (t3)
R(a)
R(b)
R(b)
W(b)
Database System Concepts - 7th Edition 17.22 ©Silberschatz, Korth and Sudarshan
If a non-serial schedule can be transformed into its corresponding serial schedule, it is said
to be serializable
Non-serial Schedule
A schedule where the transactions are overlapping or switching places. As they are used to
carry out actual database operations, multiple transactions are running at once. It’s possible
that these transactions are focusing on the same data set. Therefore, it is crucial that non-
serial schedules can be serialized in order for our database to be consistent both before and
after the transactions are executed.
Database System Concepts - 7th Edition 17.23 ©Silberschatz, Korth and Sudarshan
Conflict Serializability
Database System Concepts - 7th Edition 17.24 ©Silberschatz, Korth and Sudarshan
Conflict Serializability (Cont.)
Schedule 3 Schedule 6
Database System Concepts - 7th Edition 17.25 ©Silberschatz, Korth and Sudarshan
Conflict Serializability (Cont.)
Database System Concepts - 7th Edition 17.26 ©Silberschatz, Korth and Sudarshan
View serializability
Database System Concepts - 7th Edition 17.27 ©Silberschatz, Korth and Sudarshan
View Serializability
Database System Concepts - 7th Edition 17.29 ©Silberschatz, Korth and Sudarshan
T1: Read A → Write A→ Read B → Write B`
T2: Read B → Write B`
T3: Read C → Write C→ Read D → Write D`
T4: Read D → Write D
In order for a schedule to be considered serializable, it must first satisfy the conflict
serializability property. In our example schedule above, notice that Transaction 1 (T1) and
Transaction 2 (T2) read data item B before either writing it. This causes a conflict between T1
and T2 because they are both trying to read and write the same data item concurrently.
Therefore, the given schedule does not conflict with serializability.
there is another type of serializability called view serializability which our example does
satisfy. View serializability requires that if two transactions cannot see each other's updates
(i.e., one transaction cannot see the effects of another concurrent transaction), the schedule is
considered to view serializable. In our example, Transaction 2 (T2) cannot see any updates
made by Transaction 4 (T4) because they do not share common data items. Therefore, the
schedule is viewed as serializable.
Database System Concepts - 7th Edition 17.30 ©Silberschatz, Korth and Sudarshan
Benefits of Serializability in DBMS
Predictable execution:
Easier to Reason about & Debug
Reduced Costs:
Increased Performance
Database System Concepts - 7th Edition 17.31 ©Silberschatz, Korth and Sudarshan
Other Notions of Serializability
Database System Concepts - 7th Edition 17.32 ©Silberschatz, Korth and Sudarshan
Testing for Serializability
Database System Concepts - 7th Edition 17.33 ©Silberschatz, Korth and Sudarshan
Test for Conflict Serializability
Database System Concepts - 7th Edition 17.35 ©Silberschatz, Korth and Sudarshan
Recoverable Schedules
Need to address the effect of transaction failures on
concurrently
running transactions.
Recoverable schedule — if a transaction Tj reads a data item
previously written by a transaction Ti , then the commit
operation of Ti appears before the commit operation of Tj.
The following schedule (Schedule 11) is not recoverable
Database System Concepts - 7th Edition 17.36 ©Silberschatz, Korth and Sudarshan
Cascading Rollbacks
Database System Concepts - 7th Edition 17.37 ©Silberschatz, Korth and Sudarshan
Cascadeless Schedules
Database System Concepts - 7th Edition 17.38 ©Silberschatz, Korth and Sudarshan
Concurrency Control
Database System Concepts - 7th Edition 17.39 ©Silberschatz, Korth and Sudarshan
Concurrency Control (Cont.)
Database System Concepts - 7th Edition 17.40 ©Silberschatz, Korth and Sudarshan
Concurrency Control vs. Serializability
Tests
Database System Concepts - 7th Edition 17.41 ©Silberschatz, Korth and Sudarshan
Weak Levels of Consistency
Database System Concepts - 7th Edition 17.42 ©Silberschatz, Korth and Sudarshan
Levels of Consistency in SQL-92
Serializable — default
Repeatable read — only committed records to be read.
• Repeated reads of same record must return same value.
• However, a transaction may not be serializable – it may
find some records inserted by a transaction but not find
others.
Read committed — only committed records can be read.
• Successive reads of record may return different (but
committed) values.
Read uncommitted — even uncommitted records may be
read.
Database System Concepts - 7th Edition 17.43 ©Silberschatz, Korth and Sudarshan
Levels of Consistency
Database System Concepts - 7th Edition 17.44 ©Silberschatz, Korth and Sudarshan
Transaction Definition in SQL
Connection.TRANSACTION_SERIALIZABLE)
Database System Concepts - 7th Edition 17.45 ©Silberschatz, Korth and Sudarshan
Implementation of Isolation Levels
Locking
• Lock on whole database vs lock on items
• How long to hold lock?
• Shared vs exclusive locks
Timestamps
• Transaction timestamp assigned e.g. when a transaction
begins
• Data items store two timestamps
Read timestamp
Write timestamp
• Timestamps are used to detect out of order accesses
Multiple versions of each data item
• Allow transactions to read from a “snapshot” of the
database
Database System Concepts - 7th Edition 17.46 ©Silberschatz, Korth and Sudarshan
Transactions as SQL Statements
E.g., Transaction 1:
select ID, name from instructor where salary > 90000
E.g., Transaction 2:
insert into instructor values ('11111', 'James', 'Marketing',
100000)
Suppose
• T1 starts, finds tuples salary > 90000 using index and locks
them
• And then T2 executes.
• Do T1 and T2 conflict? Does tuple level locking detect the
conflict?
• Instance of the phantom phenomenon
Also consider T3 below, with Wu’s salary = 90000
update instructor
set salary = salary * 1.1
where name = 'Wu’
Key idea: Detect “predicate” conflicts, and use some form of
“predicate locking”
Database System Concepts - 7th Edition 17.47 ©Silberschatz, Korth and Sudarshan
End of Chapter 17
Database System Concepts - 7th Edition 17.48 ©Silberschatz, Korth and Sudarshan