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
Definition of transactions
— A transaction is a unit of a program execution that accesses
3
INTRODUCTION TO TRANSACTION PROCESSING
Single-User System: At most one
Parallel processing:
– Processes are concurrently
executed in multiple CPUs.
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
A database - collection of named data items
Granularity of data - a field, a record , or a whole disk block
Basic operations are read and write
read(A, x): assign value of database object A to variable x;
write(x , A): write value of variable x to database object A
Example: Let T1 be a transaction that transfers $500 from account A to
account B.
This transaction can be defined as :
T1
6
READ/ WRITE OPERATIONS
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
Consistency
• 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.
11
Transactions are consistent
A transaction must leave the database in valid state.
12
Isolation
Example:
13
Durability
• Once a transaction changes the database and the changes are
committed, these changes must never be lost because of subsequent
failure.
14
Concurrency Control
Isolation (+ Consistency) => Concurrency Control
15
WHY CONCURRENCY CONTROL IS NEEDED?
16
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;
17
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;
19
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
22
WHY RECOVERY IS NEEDED: (WHAT CAUSES A
TRANSACTION TO FAIL?)
A computer failure (system crash)
A transaction or system error
23
TRANSACTION STATE
Active state: the transaction is being executed. This is the initial state of
every transaction.
Partially committed state: transaction goes into partially committed
state after the end of a transaction.
Committed state: a transaction executes all its operations successfully.
Failed state: checks made by the database recovery system fails.
Aborted − If any of the checks fails and the transaction has reached a
failed state.
The database recovery module after a transaction aborts −
Re-start the transaction
Kill the transaction
24
25
Operations
Recovery manager keeps track of the following operations:
begin_transaction
read or write
end_transaction
commit_transaction
rollback (or abort)
Recovery techniques use the following operators:
Undo
Redo
26
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.
28
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.
29
Con..
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:
31
Characterizing schedules based on
• Recoverability- How good is the system at recovering from errors?
Serializable schedule
• A schedule is equivalent to some serial schedule of the same n
transactions.
Result equivalent
• Two schedules are producing the same final state of the database.
Conflict equivalent
• The order of any two conflicting operations is the same in both schedules.
33
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.
34
Schedule Notation
•A more compact notation for schedules:
r3(Y) read(Y)
Y = Y+1
write(Y)
operation data item
end
transaction commit
note: we ignore the computations on the local copies of the data when
considering schedules (they're not interesting)
35
Examples
A serial schedule is one in which the transactions do not overlap (in
time).
36
• Types of Serializability
– Conflict Serializability
– View Serializability:
Conflict serializable:
equivalence. 37
• 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.
38
Conflict Equivalence
• Two schedules are conflict equivalent if the order of any two conflicting operations is
T1: b1,r1(X),w1(X),r1(Y),w1(Y),e1,c1,
conflicting operations:
r1(X),w2(X)
T2: b2,r2(X),w2(X),e2,c2 w1(X), r2(X)
w1(X), w2(X)
39
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
40
Testing for Conflict Serializability
41
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)
45
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?
46
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?
47