Chapter 3-Transaction Processing Concepts
Chapter 3-Transaction Processing Concepts
1
Chapter Outline:
Introduction
Transaction and System Concepts
Properties of Transaction
Schedules and Recoverability
Serializability of Schedules
Transaction Support in SQL
2
• A Transaction:
– Logical unit of database processing that includes one or more
access operations (read -retrieval, write - insert or update,
delete).
• Examples include ATM transactions, credit card approvals, flight
reservations, hotel check-in, phone calls, supermarket scanning,
academic registration and billing.
• Transaction boundaries:
8
Why Concurrency Control is needed: Three cases
i. The Lost Update Problem
– This occurs when two transactions that access the
same database items have their operations
interleaved in a way that makes the value of some
database item incorrect.
9
E.g. Account with balance A=100.
T1 reads the account A
T1 withdraws 10 from A
T1 makes the update in the Database
T2 reads the account A
T2 adds 100 on A
T2 makes the update in the Database
In the above case, if done one after the other (serially) then we have no problem.
If the execution is T1 followed by T2 then A=190
If the execution is T2 followed by T1 then A=190
But if they start at the same time in the following sequence:
T1 T2
T1 reads the account A=100
Read_item(A)
T1 withdraws 10 making the balance A=90 A=A-10
T2 reads the account A=100 Read_item(A)
A=A+100
T2 adds 100 making A=200 Write_item(A)
T1 makes the update in the Database A=90 Write_item(A)
T2 makes the update in the Database A=200
After the successful completion of the operation the final value of A will be 200
which override the update made by the first transaction that changed the value from
100 to 90. 10
ii. The Temporary Update (or Dirty Read) Problem
– This occurs when one transaction updates a
database item and then the transaction fails for
some reason .
– The updated item is accessed by another
transaction before it is changed back to its original
value. Based on the above scenario:
T1 T2
Read_item(A) Fig 2: Temporal update problem
A=A+100
Write_item(A)
Read_item(A)
Transaction T2 fails and must change the
A=A-10 values of A back to its old value;
Meanwhile T1 has read the temporary
Write_item(A) incorrect value of A
Abort
T2 increases A Commit
making it 200 but then aborts the transaction before it
is committed. T1 gets 200, subtracts 10 and make it 190. But the actual
balance should be 90
11
iii. 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.
•Example: T1 would like to add the values of A=10, B=20 and C=30. after
the values are read by T1 and before its completion, T2 updates the value
of B to be 50. at the end of the execution of the two transactions T1 will
come up with the sum of 60 while it should be 90 since B is updated to 50
T1 T2
Sum= 0;
Read_item(A)
Sum=Sum+A
Read_item(B)
Sum=Sum+B
Read_item(B)
B=50
Read_item(C)
Sum=Sum+C
12
Why recovery is needed:
-Whenever a transaction is submitted to the DBMS for execution, the
system is responsible for making sure that either all operations in
the transaction to be completed successfully or the transaction has
no effect on the database or any other transaction.
-The DBMS may permit some operations of a transaction T to be
applied to the database but a transaction may fails after executing
some of its operations
• What causes a Transaction to fail
1. A computer failure (system crash):
A hardware or software error occurs in the computer system
during transaction execution. If the hardware crashes, the
contents of the computer’s internal 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. 13
3. Exception conditions detected by the transaction:
– Certain conditions forces cancellation of the transaction.
– For example, data for the transaction may not be found.
such as insufficient account balance in a banking
database, may cause a transaction, such as a fund
withdrawal from that account, to be canceled.
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 (see Chapter 2).
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 may happen during a read or a write operation
of the transaction.
6. Physical problems and catastrophes:
This refers to an endless list of problems that includes
power or air-conditioning failure, fire, theft, overwriting
disks or tapes by mistake 14
2. Transaction and System Concepts
• A transaction is an atomic unit of work that is either
completed in its entirety or not done at all.
– For recovery purposes, the system needs to keep track
of when the transaction starts, terminates, and commits
or aborts.
• Transaction states:
– Active state -indicates the beginning of a transaction
execution
– Partially committed state shows the end of read/write
operation but this will not ensure permanent modification
on the data base
– Committed state -ensures that all the changes done on a
record by a transition were done persistently
– Failed state happens when a transaction is aborted
during its active state or if one of the rechecking is fails
– Terminated State -corresponds to the transaction leaving
the system
15
State transition diagram illustrating the states for
transaction execution
16
– T in the following discussion refers to a unique
transaction-id that is generated automatically by the
system and is used to identify each transaction:
– Types of log record:
• [start_transaction,T]: Records that transaction T
has started execution.
• [write_item,T,X,old_value,new_value]: Records that
transaction T has changed the value of database
item X from old_value to new_value.
• [read_item,T,X]: Records that transaction T has
read the value of database item X.
• [commit,T]: Records that transaction T has
completed successfully, and affirms that its effect
can be committed (recorded permanently) to the
database.
• [abort,T]: Records that transaction T has been
aborted.
17
3. Desirable Properties of Transactions
To ensure data integrity , DBMS should maintain the
following ACID properties:
• Atomicity: A transaction is an atomic unit of processing; it
is either performed in its entirety or not performed at all.
• Consistency preservation: A correct execution of the
transaction must take the database from one consistent
state to another.
• Isolation: A transaction should not make its updates visible
to other transactions until it is committed; this property,
when enforced strictly, solves the temporary update
problem and makes cascading rollbacks of transactions
unnecessary
• Durability or permanency: Once a transaction changes
the database and the changes are committed, these
changes must never be lost because of subsequent failure.
18
Example:
• Suppose that Ti is a transaction that transfer 200 birr from account
CA2090( which is 5,000 Birr) to SB2359(which is 3,500 birr) as follows
• Read(CA2090)
• CA2090= CA2090-200
• Write(CA2090)
• Read(SB2359)
• SB2359= SB2359+200
• Write(SB2359)
• Atomicity- either all or none of the above operation will be done – this is
materialized by transaction management component of DBMS
• Consistency-the sum of CA2090 and SB2359 be unchanged by the
execution of Ti i.e 8500- this is the responsibility of application
programmer who codes the transaction
• Isolation- when several transaction are being processed concurrently
on a data item they may create many inconsistent problems. So
handling such case is the responsibility of Concurrency control
component of the DBMS
• Durability - once Ti writes its update this will remain there when the
database restarted from failure . This is the responsibility of recovery
management components of the DBMS 19
4.Characterizing Schedules
Sa : r2(X);w2(X);r1(X);w1(X);a2; A=A+100
Write_item(A)
Read_item(A)
A=A-10 20
Write_item(A)
Two operations in a schedule are side to be conflict if they
satisfy all three of the following conditions:
They belongs to different transaction
They access the same data item X
At least one of the operation is a write_Item(X)
Eg. Sa: r1(X); r2(x); w1(X); r1(Y); W2(X); W1(Y);
r1(X) and w2(X)
r2(X) and w1(X); Conflicting
W1(X) and w2(X) operations
21
i. Schedules classified by recoverability:
• Recoverable schedule:
– One where no committed transaction needs to be rolled back.
– A schedule S is recoverable if no transaction T in S commits until
all transactions T’ that have written an item that T reads have
committed. Examples,
• Sc: r1(X); w1(X); r2(X); r1(Y);w2(x);c2;a1; not recoverable able
v er
• Sd: r1(X); w1(X); r2(X); r1(Y); w2(X);w1(Y); c1; c2; co
e
• Se: r1(X); w1(X); r2(X); r1(Y); w2(x) ; w1(Y); a1; a2; R
• Cascadeless schedule:
– One where every transaction reads only the items that were
written by committed transactions. Eg.
• Sf: r1(X); w1(X); r1(Y); c1; r2(X); w2(X);w1(Y); c2;
• Strict Schedules:
– A schedule in which a transaction can neither read nor write an
item X until the last transaction that wrote X has
committed/aborted.
22
– Eg. Sg: w1(X,5) ; c1; w2(x,8);
ii. Characterizing Schedules based on Serializability
– The concept of Serializable of schedule is used to identify
which schedules are correct when concurrent transactions
executions have interleaving of their operations in the
schedule
• Serial schedule:
– A schedule S is serial if, for every transaction T
participating in the schedule, all the operations of T are
executed consecutively in the schedule. Otherwise, the
schedule is called nonserial schedule.
– For example, in the banking example suppose there are
two transaction where one transaction calculate the
interest on the account and another deposit some
money into the account. hence the order of execution
is important for the final result.
23
Serializability Example
– In example below, see that transaction 1, now
has 1 new row.(Transaction without Scheduling)
24
Serializability Example
– Example as shown in the next slide, the
row selected by one transaction cannot be
changed by another until the first
transaction finishes. (Transaction without
Scheduling)
– In example below, we can see that when
we tried to insert a new row in different
transactions, Lock wait timeout exceeded error.
25
Serializability Example
26