Chapter 3 Transaction Processing Conceptes
Chapter 3 Transaction Processing Conceptes
Systems
Course Code: CoSc2042
Chapter Three
Transaction Processing Concepts
By Denekew Tadele (Msc.)
Tel: +251913793506
Email: [email protected]
1
Contents
Introduction
Transaction and System Concepts
Properties of Transaction
Schedules and Recoverability
Serializability of Schedules
Transaction Support in SQL
2
Introduction to Transaction Processing
• Transaction: An executing program (process) that
includes one or more database access operations
– Read operations (database retrieval, such as SQL SELECT)
– Write operations (modify database, such as SQL INSERT, UPDATE,
DELETE)
– Transaction: A logical unit of database processing
– Example: Bank balance transfer of $100 dollars from a checking
account to a saving account in a BANK database
• Note: Each execution of a program is a distinct transaction with
different parameters
– Bank transfer program parameters: savings account number,
checking account number, transfer amount
3
Introduction to Transaction Processing (cont.)
• A transaction (set of operations) may be:
– stand-alone, specified in a high level language like SQL
submitted interactively, or
– consist of database operations embedded within a
program (most transactions)
• Transaction boundaries: Begin and End transaction.
– Note: An application program may contain several
transactions separated by Begin and End transaction
boundaries
4
Introduction to Transaction Processing (cont.)
• Transaction Processing Systems: Large multi-user
database systems supporting thousands of
concurrent transactions (user processes) per
minute
• Two Modes of Concurrency
– Interleaved processing: concurrent execution of
processes is interleaved in a single CPU
– Parallel processing: processes are concurrently
executed in multiple CPUs (Figure 3.1)
– Basic transaction processing theory assumes
interleaved concurrency
5
Figure 3.1
Introduction to Transaction Processing (cont.)
For transaction processing purposes, a simple
database model is used:
• A database - collection of named data items
• Granularity (size) of a data item - a field (data item
value), a record, or a whole disk block
– TP concepts are independent of granularity
• Basic operations on an item X:
– read_item(X): Reads a database item named X
into a program variable. To simplify our notation,
we assume that the program variable is also
named X.
– write_item(X): Writes the value of program
variable X into the database item named X.
7
Introduction to Transaction Processing (cont.)
8
Introduction to Transaction Processing
(cont.)
READ AND WRITE OPERATIONS (cont.):
write_item(X) command includes the following
steps:
• Find the address of the disk block that contains
item X.
• Copy that disk block into a buffer in main memory
(if it is not already in some main memory buffer).
• Copy item X from the program variable named X
into its correct location in the buffer.
• Store the updated block from the buffer back to
disk (either immediately or at some later point in
time).
9
Transaction Notation
10
Figure 3.2
Why we need concurrency control
Without Concurrency Control, problems may occur
with concurrent transactions:
• Lost Update Problem.
Occurs when two transactions update the same data
item, but both read the same original value before
update (Figure 3.3(a), next slide)
• The Temporary Update (or Dirty Read) Problem.
This occurs when one transaction T1 updates a
database item X, which is accessed (read) by
another transaction T2; then T1 fails for some reason
(Figure 3.3(b)); X was (read) by T2 before its value is
changed back (rolled back or UNDONE) after T1 fails
12
Figure 3.3
Why we need concurrency
control (cont.)
• The Incorrect Summary Problem .
One transaction is calculating an aggregate summary
function on a number of records (for example, sum
(total) of all bank account balances) while other
transactions are updating some of these records (for
example, transferring a large amount between two
accounts, see Figure 3.3(c)); the aggregate function
may read some values before they are updated and
others after they are updated.
14
Why we need concurrency
control (cont.)
• The Unrepeatable Read Problem .
A transaction T1 may read an item (say, available
seats on a flight); later, T1 may read the same item
again and get a different value because another
transaction T2 has updated the item (reserved seats
on the flight) between the two reads by T1
16
Why recovery is needed
Causes of transaction failure:
1. A computer failure (system crash): A hardware or
software error occurs during transaction execution. If
the hardware crashes, the contents of the computer’s
internal main memory may be lost.
2. A transaction or system error : Some operation in the
transaction may cause it to fail, such as integer overflow
or division by zero. Transaction failure may also occur
because of erroneous parameter values or because of
a logical programming error. In addition, the user may
interrupt the transaction during its execution.
17
Why recovery is needed (cont.)
3.Local errors or exception conditions detected by the
transaction:
- certain conditions necessitate cancellation of the
transaction. For example, data for the transaction may
not be found. A condition, such as insufficient account
balance in a banking database, may cause a
transaction, such as a fund withdrawal, to be canceled
- a programmed abort causes the transaction to fail.
4.Concurrency control enforcement: The concurrency
control method may decide to abort the transaction, to
be restarted later, because it violates Serializability or
because several transactions are in a state of deadlock.
18
Why recovery is needed (cont.)
5. Disk failure: Some disk blocks may lose their data
because of a read or write malfunction or because of
a disk read/write head crash. This kind of failure and
item 6 are more severe than items 1 through 4.
6. Physical problems and catastrophes: This refers
to an endless list of problems that includes power or
air-conditioning failure, fire, theft, sabotage,
overwriting disks or tapes by mistake, and mounting
of a wrong tape by the operator.
19
Transaction and System Concepts
A transaction is an atomic unit of work that is either
completed in its entirety or not done at all. A transaction
passes through several states (Figure 3.4, similar to
process states in operating systems).
Transaction states:
• Active state (executing read, write operations)
• Partially committed state (ended but waiting for
system checks to determine success or failure)
• Committed state (transaction succeeded)
• Failed state (transaction failed, must be rolled back)
• Terminated State (transaction leaves system)
20
Figure 3.4: State transition diagram illustrating the states for transaction execution.
Transaction and System Concepts (cont.)
DBMS Recovery Manager needs system to keep track of
the following operations (in the system log file):
• begin_transaction: Start of transaction execution.
• read or write: Read or write operations on the database
items that are executed as part of a transaction.
• end_transaction: Specifies end of read and write
transaction operations have ended. System may still
have to check whether the changes (writes) introduced
by transaction can be permanently applied to the
database (commit transaction); or whether the
transaction has to be rolled back (abort transaction)
because it violates concurrency control or for some other
reason.
22
Transaction and System Concepts (cont.)
23
Transaction and System Concepts (cont.)
24
Transaction and System Concepts (cont.)
25
Transaction and System Concepts (cont.)
Types of records (entries) in log file:
• [start_transaction,T]: Records that transaction T has
started execution.
• [write_item,T,X,old_value,new_value]: T has changed
the value of item X from old_value to new_value.
• [read_item,T,X]: T has read the value of item X (not
needed in many cases).
• [end_transaction,T]: T has ended execution
• [commit,T]: T has completed successfully, and
committed.
• [abort,T]: T has been aborted.
26
Transaction and System Concepts (cont.)
27
Transaction and System Concepts (cont.)
Commit Point of a Transaction (cont.):
Log file buffers: Like database files on disk, whole disk blocks
must be read or written to main memory buffers.
For log file, the last disk block (or blocks) of the file will be in main
memory buffers to easily append log entries at end of file.
Force writing the log buffer: before a transaction reaches its
commit point, any main memory buffers of the log that have not
been written to disk yet must be copied to disk.
Called force-writing the log buffers before committing a
transaction.
Needed to ensure that any write operations by the transaction are
recorded in the log file on disk before the transaction commits
28
Desirable Properties of Transactions
Called ACID properties – Atomicity,
Consistency, Isolation, Durability:
• Atomicity: A transaction is an atomic unit of
processing; it is either performed in its entirety
or not performed at all.
29
Desirable Properties of Transactions (cont.)
30
Desirable Properties of Transactions (cont.)
31
Schedules of Transactions
• Transaction schedule (or history): When transactions are
executing concurrently in an interleaved fashion, the order of
execution of operations from the various transactions forms
what is known as a transaction schedule (or history).
32
Figure 3.5
Schedules of Transactions (cont.)
• Schedules can also be displayed in more compact notation
• Order of operations from left to right
• Include only read (r) and write (w) operations, with
transaction id (1, 2, …) and item name (X, Y, …)
• Can also include other operations such as b (begin), e (end), c
(commit), a (abort)
• Schedules in Figure 3.5 would be displayed as follows:
– Schedule A: r1(X); w1(X); r1(Y); w1(Y); r2(X); w2(x);
– Schedule B: r2(X); w2(X); r1(X); w1(X); r1(Y); w1(Y);
– Schedule C: r1(X); r2(X); w1(X); r1(Y); w2(X); w1(Y);
– Schedule D: r1(X); w1(X); r2(X); w2(X); r1(Y); w1(Y);
34
Schedules of Transactions (cont.)
• Formal definition of a schedule (or history) S of n
transactions T1, T2, ..., Tn :
An ordering of all the operations of the transactions subject
to the constraint that, for each transaction Ti that participates
in S, the operations of Ti in S must appear in the same order
in which they occur in Ti.
35
Schedules of Transactions (cont.)
• For n transactions T1, T2, ..., Tn, where each Ti has mi read
and write operations, the number of possible schedules is (! is
factorial function):
(m1 + m2 + … + mn)! / ( (m1)! * (m2)! * … * (mn)! )
37
Characterizing Schedules Based on
Recoverability (cont.)
• Example: Schedule A below is non-recoverable because T2
reads the value of X that was written by T1, but then T2
commits before T1 commits or aborts
• To make it recoverable, the commit of T2 (c2) must be
delayed until T1 either commits, or aborts (Schedule B)
• If T1 commits, T2 can commit
• If T1 aborts, T2 must also abort because it read a value that
was written by T1; this value must be undone (reset to its old
value) when T1 is aborted
– known as cascading rollback
• Schedule A: r1(X); w1(X); r2(X); w2(X); c2; r1(Y); w1(Y); c1 (or a1)
• Schedule B: r1(X); w1(X); r2(X); w2(X); r1(Y); w1(Y); c1 (or a1); ...
38
Characterizing Schedules based on
Recoverability (cont.)
Recoverable schedules can be further refined:
• Cascadeless schedule: A schedule in which a transaction
T2 cannot read an item X until the transaction T1 that last
wrote X has committed.
• The set of cascadeless schedules is a subset of the set of
recoverable schedules.
39
Characterizing Schedules Based on
Recoverability (cont.)
• Example: Schedule B below is not cascadeless because T2
reads the value of X that was written by T1 before T1
commits
• If T1 aborts (fails), T2 must also be aborted (rolled back)
resulting in cascading rollback
• To make it cascadeless, the r2(X) of T2 must be delayed until
T1 commits (or aborts and rolls back the value of X to its
previous value) – see Schedule C
40
Characterizing Schedules based on
Recoverability (cont.)
Cascadeless schedules can be further refined:
• Strict schedule: A schedule in which a transaction T2 can
neither read nor write an item X until the transaction T1 that
last wrote X has committed.
• The set of strict schedules is a subset of the set of cascadeless
schedules.
• If blind writes are not allowed, all cascadeless schedules are
also strict
41
Characterizing Schedules Based on
Recoverability (cont.)
• Example: Schedule C below is cascadeless and also strict
(because it has no blind writes)
• Schedule D is cascadeless, but not strict (because of the blind
write w3(X), which writes the value of X before T1 commits)
• To make it strict, w3(X) must be delayed until after T1
commits – see Schedule E
42
Characterizing Schedules Based on
Recoverability (cont.)
Summary:
• Many schedules can exist for a set of transactions
• The set of all possible schedules can be partitioned into two
subsets: recoverable and non-recoverable
• A subset of the recoverable schedules are cascadeless
• If blind writes are allowed, a subset of the cascadeless
schedules are strict
• If blind writes are not allowed, the set of cascadeless
schedules is the same as the set of strict schedules
43
Characterizing Schedules based on
Serializability
• Among the large set of possible schedules, we want to
characterize which schedules are guaranteed to give a
correct result
• The consistency preservation property of the ACID
properties states that: each transaction if executed on its
own (from start to finish) will transform a consistent
state of the database into another consistent state
• Hence, each transaction is correct on its own
44
Characterizing Schedules based on
Serializability (cont.)
• Serial schedule: A schedule S is serial if, for every
transaction T participating in the schedule, all the
operations of T are executed consecutively (without
interleaving of operations from other transactions) in the
schedule. Otherwise, the schedule is called nonserial.
• Based on the consistency preservation property, any
serial schedule will produce a correct result (assuming no
inter-dependencies among different transactions)
45
Characterizing Schedules based on
Serializability (cont.)
• Serial schedules are not feasible for performance
reasons:
– No interleaving of operations
– Long transactions force other transactions to wait
– System cannot switch to other transaction when a
transaction is waiting for disk I/O or any other event
– Need to allow concurrency with interleaving without
sacrificing correctness
46
Characterizing Schedules based on
Serializability (cont.)
• Serializable schedule: A schedule S is serializable if it is
equivalent to some serial schedule of the same n
transactions.
• There are (n)! serial schedules for n transactions – a
serializable schedule can be equivalent to any of the
serial schedules
• Question: How do we define equivalence of schedules?
47
Equivalence of Schedules
• Result equivalent: Two schedules are called result
equivalent if they produce the same final state of the
database.
• Difficult to determine without analyzing the internal
operations of the transactions, which is not feasible in
general.
• May also get result equivalence by chance for a
particular input parameter even though schedules are
not equivalent in general (see Figure 3.6, next slide)
48
Figure 3.6
Equivalence of Schedules (cont.)
• Conflict equivalent: Two schedules are conflict
equivalent if the relative order of any two conflicting
operations is the same in both schedules.
• Commonly used definition of schedule equivalence
• Two operations are conflicting if:
– They access the same data item X
– They are from two different transactions
– At least one is a write operation
• Read-Write conflict example: r1(X) and w2(X)
• Write-write conflict example: w1(Y) and w2(Y)
50
Equivalence of Schedules (cont.)
• Changing the order of conflicting operations generally
causes a different outcome
• Example: changing r1(X); w2(X) to w2(X); r1(X) means
that T1 will read a different value for X
• Example: changing w1(Y); w2(Y) to w2(Y); w1(Y) means
that the final value for Y in the database can be different
• Note that read operations are not conflicting; changing
r1(Z); r2(Z) to r2(Z); r1(Z) does not change the outcome
51
Characterizing Scedules Based on
Serializability (cont.)
• Conflict equivalence of schedules is used to determine
which schedules are correct in general (serializable)
52
Characterizing Schedules based on
Serializability (cont.)
• A serializable schedule is considered to be correct
because it is equivalent to a serial schedule, and any
serial schedule is considered to be correct
– It will leave the database in a consistent state.
– The interleaving is appropriate and will result in a
state as if the transactions were serially executed, yet
will achieve efficiency due to concurrent execution
and interleaving of operations from different
transactions.
53
Characterizing Schedules based
on Serializability (cont.)
• Serializability is generally hard to check at run-
time:
– Interleaving of operations is generally handled by
the operating system through the process
scheduler
– Difficult to determine beforehand how the
operations in a schedule will be interleaved
– Transactions are continuously started and
terminated
54
Characterizing Schedules Based on
Serializability (cont.)
Practical approach:
• Come up with methods (concurrency control
protocols) to ensure serializability (discussed in
Chapter 4)
• DBMS concurrency control subsystem will enforce the
protocol rules and thus guarantee serializability of
schedules
• Current approach used in most DBMSs:
– Use of locks with two phase locking (see Section 4.1)
55
Characterizing Schedules based on
Serializability (cont.)
Testing for conflict serializability
Algorithm 3.1:
• Looks at only r(X) and w(X) operations in a schedule
• Constructs a precedence graph (serialization graph) – one
node for each transaction, plus directed edges
• An edge is created from Ti to Tj if one of the operations in Ti
appears before a conflicting operation in Tj
• The schedule is serializable if and only if the precedence graph
has no cycles.
56
Algorithm 3.1.
Figure 3.7
Figure 3.5
Figure 3.8
Figure 3.8
Characterizing Schedules based on
Serializability (cont.)
• View equivalence: A less restrictive definition of
equivalence of schedules than conflict serializability
when blind writes are allowed
61
Characterizing Schedules based
on Serializability (cont.)
Two schedules are said to be view equivalent if the following
three conditions hold:
• The same set of transactions participates in S and S’, and S
and S’ include the same operations of those transactions.
• For any operation Ri(X) of Ti in S, if the value of X read was
written by an operation Wj(X) of Tj (or if it is the original
value of X before the schedule started), the same condition
must hold for the value of X read by operation Ri(X) of Ti in
S’.
• If the operation Wk(Y) of Tk is the last operation to write
item Y in S, then Wk(Y) of Tk must also be the last operation
to write item Y in S’.
62
Characterizing Schedules based
on Serializability (cont.)
The premise behind view equivalence:
Each read operation of a transaction reads the result
of the same write operation in both schedules.
“The view”: the read operations are said to see the
the same view in both schedules.
The final write operation on each item is the same
on both schedules resulting in the same final
database state in case of blind writes
63
Characterizing Schedules based on
Serializability (cont.)
Relationship between view and conflict equivalence:
The two are same under constrained write
assumption (no blind writes allowed)
Conflict serializability is stricter than view
serializability when blind writes occur (a schedule
that is view serializable is not necessarily conflict
serialiable.
Any conflict serializable schedule is also view
serializable, but not vice versa.
64
Characterizing Schedules based on
Serializability (cont.)
Relationship between view and conflict equivalence
(cont):
Consider the following schedule of three transactions
T1: r1(X); w1(X); T2: w2(X); and T3: w3(X):
Schedule Sa: r1(X); w2(X); w1(X); w3(X); c1; c2; c3;
In Sa, the operations w2(X) and w3(X) are blind writes, since T2
and T3 do not read the value of X.
65
Characterizing Schedules based on
Serializability (cont.)
Other Types of Equivalence of Schedules
Under special semantic constraints, schedules that
are otherwise not conflict serializable may work
correctly
Using commutative operations of addition and
subtraction (which can be done in any order) certain
non-serializable transactions may work correctly;
known as debit-credit transactions
66
Characterizing Schedules based on
Serializability (cont.)
Other Types of Equivalence of Schedules (cont.)
Example: bank credit/debit transactions on a given item are
separable and commutative.
Consider the following schedule S for the two transactions:
Sh : r1(X); w1(X); r2(Y); w2(Y); r1(Y); w1(Y); r2(X); w2(X);
Using conflict serializability, it is not serializable.
However, if it came from a (read,update, write) sequence as
follows:
r1(X); X := X – 10; w1(X); r2(Y); Y := Y – 20; w2(Y); r1(Y);
Y := Y + 10; w1(Y); r2(X); X := X + 20; w2(X);
Sequence explanation: debit, debit, credit, credit.
It is a correct schedule for the given semantics
67
Introduction to Transaction Support in
SQL
• A single SQL statement is always considered to be
atomic. Either the statement completes
execution without error or it fails and leaves the
database unchanged.
• With SQL, there is no explicit Begin Transaction
statement. Transaction initiation is done implicitly
when particular SQL statements are encountered.
• Every transaction must have an explicit end
statement, which is either a COMMIT or
ROLLBACK.
68
Introduction to Transaction Support in
SQL (cont.)
70
Transaction Support in SQL (cont.)
Potential problem with lower isolation levels:
Dirty Read: Reading a value that was written by a
transaction that failed.
Nonrepeatable Read: Allowing another transaction to
write a new value between multiple reads of one
transaction.
A transaction T1 may read a given value from a table. If
another transaction T2 later updates that value and
then T1 reads that value again, T1 will see a different
value. Example: T1 reads the No. of seats on a flight.
Next, T2 updates that number (by reserving some seats).
If T1 reads the No. of seats again, it will see a different
value.
71
Transaction Support in SQL (cont.)
72
Transaction Support in SQL2 (cont.)
73
Table 3.1
Chapter 3 Summary
Introduction to Transaction Processing
Transaction and System Concepts
Desirable Properties of Transactions (ACID
properties)
Characterizing Schedules based on
Recoverability
Characterizing Schedules based on
Serializability
Transaction Support in SQL
75