Chapter - 3 TRANSACTION PROCESSING
Chapter - 3 TRANSACTION PROCESSING
Chapter – 3
TRANSACTION PROCESSING
1
Chapter Outline
1. Introduction to Transaction Processing
2. Transaction and System Concepts
2
INTRODUCTION TO TRANSACTION PROCESSING
Single-User System: At most one
Parallel processing:
– Processes are concurrently
executed in multiple CPUs.
3
Definition of transactions
— A transaction is a unit of a program execution that accesses
4
Transaction boundaries:
Begin and End transaction.
Suppose a bank employee transfers $500 from A‘s account to B's account.
This very simple and small transaction involves several low-level tasks.
5
Simple Model of a Database
Basic unit of data transfer from the disk to the computer main
memory is one block.
In general, a data item (what is read or written) will be the field of
some record in the database, although it may be a larger unit such as
a record or even a whole block.
7
write_item(X) command includes the following steps:
Copy that disk block into a buffer in main memory (if that disk
Copy item X from the program variable named X into its correct
Store the updated block from the buffer back to disk (either
8
PROPERTIES OF TRANSACTIONS
9
Atomic transactions
10
Atomic Transaction
11
Consistency
• Consistency preservation: A correct execution of the transaction
must take the database from one consistent state to another.
•Example: Wilma tries to withdraw $1000 from account 387.
12
Transactions are consistent
A transaction must leave the database in valid or consistent
state.
valid state == no constraint violations
A constraint is a declared rule defining /specifying database
states
Constraints may be violated temporarily …
but must be corrected before the transaction completes.
13
Isolation
• Transactions are isolated:
• If a transaction should not make its updates visible to other transactions until it
is committed; this property, solves the temporary update problem
• If two transactions occur at the same time, the cumulative effect must be the
same as if they had been done in isolation.
• Ensuring isolation is the task of concurrency control.
14
Isolation
•Fred is withdrawing $500 from account 543.
•Wilma’s employer is depositing $1983.23 to account 543.
•These transactions are happening at the same time.
15
Durability
•Transaction are durable;
• Once a transaction changes the database and the changes are
committed, these changes must never be lost because of subsequent
failure.
• Once a transaction's effect on the database state has been committed,
it must be permanent.
• The DBMS must ensure persistence, even in the event of system failures.
•Sources of failure:
– computer or operating system crash
– disk failure
– fire, theft, power outage, earthquake, operator errors, …
16
Durability
•Wilma deposits $50,000 to account 387.
•Later, the bank’s computer crashes due to a lightning storm.
17
Concurrency Control
Isolation (+ Consistency) => Concurrency Control
18
WHY CONCURRENCY CONTROL IS NEEDED?
19
Example
The Lost Update Problem
T1 T2 State of X
read_item(X); 20
X:= X+10; read_item(X); 20
X:= X+20;
write_item(X); 40
commit;
Lost update write_item(X); 30
commit;
20
The Temporary Update (or Dirty Read) Problem.
T1 T2 State of X sum
read_item(X); 20 0
X:= X+10;
Dirty update write_item(X);
read_ item(X); 30
sum:= sum+X;
write_item(sum); 30
X:=X+10; commit;
write_item(X); 40
commit;
22
The Incorrect Summary Problem
If one transaction is calculating an aggregate summary function on a
number of records while other transactions are updating some of these
records, the aggregate function may calculate some values before they
are updated and others after they are updated.
read_item(A); 0
sum:= sum+A;
write_item(A);
commit; 100
read_item(X); 30
X:= X-10;
write_item(X);
commit; read_item(X); 20
sum:= sum+X;
read_item(Y); 10
sum:= sum+Y;
read_item(Y); 10
Y:= Y+10;
write_item(Y); 20
commit;
Incorrect summary
25
WHY RECOVERY IS NEEDED: (WHAT CAUSES A
TRANSACTION TO FAIL?)
A computer failure (system crash)
A transaction or system error
26
TRANSACTION STATE
27
Con…
Failed state: A transaction is said to be in a failed state if any of the
checks made by the database recovery system fails.
Aborted − If any of the checks fails and the transaction has reached a
failed state.
28
29
Operations
30
THE SYSTEM LOG
–[read_item, T, X] - Indicates that transaction T has read the value of database item
X.
–[commit, T] - Indicates that transaction T has completed successfully, and affirms that
its effect can be committed (recorded permanently) to the database.
32
Commit Point of a Transaction
Definition: a transaction T reaches its commit point when,
all its operations accessing DB are executed successfully and
changes are recorded in the log.
Beyond the commit point, the transaction is said to be
committed, and its effect is assumed to be permanently recorded
in the database.
The transaction then writes an entry [commit,T] into the log.
33
Undo (Roll Back) of transactions:
Needed for transactions that have a [start_transaction,T] entry to the log
but no commit entry [commit,T] into the log.
Redoing transactions:
35
Schedules classified on recoverability:
Recoverable schedule- no transaction needs to be rolled back.
Cascadeless schedule - every transaction reads only the items
that are written by committed transactions.
Schedules requiring cascaded rollback – uncommitted
transactions that read an item from a failed transaction must be
rolled back.
Strict Schedules - a transaction can neither read nor write an
item X until the last transaction that wrote X has committed.
36
Characterizing schedules based on Serializability
Serial schedule: A schedule S is serial if, Transactions are ordered one
after the other. Otherwise, the schedule is called nonserial schedule.
Serializable schedule: A schedule S is Serializable,if it is equivalent to
some serial schedule of the same n transactions.
Result equivalent
• Two schedules are called result equivalent if they produce the same final
state of the database.
Conflict equivalent
• Two schedules are said to be conflict equivalent if the order of any two
conflicting operations is the same in both schedules.
37
Figure 3.2 Examples of serial and nonserial schedules involving transactions
T1 and T2. (a) Serial schedule A: T1 followed by T2. (b) Serial schedule B: T2
followed by T1. (c) Two nonserial schedules C and D with interleaving of
operations.
38
Schedule Notation
39
Examples
A serial schedule is one in which the transactions do not overlap (in
time).
40
• Types of Serializability
– Conflict Serializability
– View Serializability:
Conflict serializable:
equivalence. 41
• Interleaving of operations occurs in an operating system through some
scheduler
• Difficult to determine before hand how the operations in a schedule will be
interleaved.
42
Conflict Equivalence
• Two schedules are conflict equivalent if the order of any two conflicting operations
is the same in both schedules.
• Two operations conflict
– they access the same data item (read or write)
– if they belong to different transactions
– at least one is a write
T1: b1,r1(X),w1(X),r1(Y),w1(Y),e1,c1,
T2: b2,r2(X),w2(X),e2,c2 conflicting operations:
r1(X),w2(X)
w1(X), r2(X)
w1(X), w2(X)
43
Example: Conflict Equivalence
schedule 1:
b1,r1(X),w1(X),r1(Y),w1(Y),e1,c1,
b2,r2(X),w2(X),e2,c2
schedule 2: r1(X) < w2(X), w1(X) < r2(X), w1(X) <
w2(X)
b2,r2(X),w2(X),
b1,r1(X),w1(X),r1(Y),w1(Y),e1,c1, e2,c2
w2(X) < r1(X), r2(X) < w1(X), w2(X) < w1(X)
schedule 3:
b1,r1(X),w1(X),
b2,r2(X),w2(X),e2,c2, r1(Y),w1(Y),e1,c1,
r1(X) < w2(X), w1(X) < r2(X), w1(X) < w2(X)
Schedule1and schedule 3 are conflict equivalent schedule 2 is not
conflict equivalent to either schedule 1 or 3
44
Testing for Conflict Serializability
• Precedence graphs are a more efficient test
– graph indicates a partial order on the transactions required
by the order of the conflicting operations.
– the partial order must hold in any conflict equivalent serial
schedule
– if there is a loop in the graph, the partial order is not
possible in any serial schedule
– if the graph has no loops, the schedule is conflict serializable
45
Precedence Graph Examples: find the graph the conflict operation between the transactions?
schedule 3:
b1,r1(X),w1(X),
b2,r2(X),w2(X),e2,c2, r1(Y),w1(Y),e1,c1,
Find the conflict operations ?
r1(X) < w2(X), w1(X) < r2(X), w1(X) < w2(X)
T1 T2
r2(X) < w1(X)
T1 T2
r2(X) < w1(X)
49
Ch-3 Assignment
Q1. Using the precedence graph as a method of checking
Serializability based on this find the following questions
S: r1(x) r2(z) r3(x) r1(z) r2(y) r3(y) w1(x) w2(z) w3(y) w2(y)
e1,c1,e2,c2,e3,c3
A. Find the Ordering of conflicting operations?
B. Is this schedule serializable?
C. Is the schedule correct?
50
Q2. Consider the schedule given below, in which, transaction T1 transfers
money from account A to account B and in the meantime, transaction T2
calculates the sum of 3 accounts namely, A, B, and C. The third column shows
the account balances and calculated values after every instruction is executed.
Discuss what problem is found in the schedule and what will be the correct
value of Accounts A, B & C averages?
51