0% found this document useful (0 votes)
13 views

Chapter 3-Transaction Processing Concepts

i need it for purpose

Uploaded by

chuol lock
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Chapter 3-Transaction Processing Concepts

i need it for purpose

Uploaded by

chuol lock
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Advanced Database Systems

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:

– Any single transaction in an application program is bounded with


Begin and End statements.
• An application program may contain several transactions
separated by the Begin and End transaction boundaries.
3
SIMPLE MODEL OF A DATABASE :
• A database is a collection of named data items
• Granularity of data - a field, a record , or a
whole disk block that measure the size of the
data item
• Basic operations that a transaction can perform
are read and write
– 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.
4
• Basic unit of data transfer from the disk to the computer main
memory is one block.
• read_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 that
disk block is not already in some main memory buffer).
– Copy item X from the buffer to the program variable
named X.
• 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 that
disk block 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).
7
• The DBMS maintains a number of buffers in the main
memory that holds database disk blocks which contains the
database items being processed.
– When this buffers are occupied and
– if there is a need for additional database block to be
copied to the main memory ;
• some buffer management policy is used to choose for
replacement but if the chosen buffer has been modified , it
must be written back to disk before it is used.
• Two sample transactions:
– (a) Transaction T1
– (b) Transaction T2

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

• 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).
• A schedule S of n transactions T1, T2, …, Tn:
– It is an ordering of the operations of the transactions
subject to the constraint that, for each transaction Ti
that participates in S, the operations of T1 in S must
appear in the same order in which they occur in Ti.
– Note, however, that operations from other
transactions Tj can be interleaved with the operations
of Ti in S. Eg. Consider the following example: T1 T2
Read_item(A)

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

 r1(X) and r2(X) No Conflict,


 W2(X) and w1(Y) why?
 R1(x) and w1(x)

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

You might also like