ADBChapter 2
ADBChapter 2
Transaction
1
What is a Transaction?
• A Transaction:
– Logical unit of database processing that includes one or
more access operations (read -retrieval, write - insert or
update, delete).
• A transaction (set of operations) may be stand-alone specified
in a high level language like SQL submitted interactively, or may
be embedded within an application program.
• 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
2
Transaction operations
• the basic database access operations that a
transaction can include are as follows:
■ read_item(X).
Reads a database item named X into a program
variable
■ write_item(X).
Writes the value of program variable X into the
database item named X.
• The basic access is one disk block from disk to
memory
3
read_item(X)
• Executing a read_item(X) command includes
the following steps:
1. Find the address of the disk block that
contains x
2. Copy the disk block to memory buffer(if not
in memory)
3. Copy the item from the buffer to program
variable x
4
write_item(X)
• Executing a write_item(X) command includes
the following steps:
1. Find the address of the disk block that
contains x
2. Copy the disk block to memory buffer (if not
in memory)
3. Copy the program variable x into buffer
4. Store the updated buffer to disk
5
A Transaction: An Informal Example
• E.g. Transfer transaction to transfer 500 birr
from account A to account B: For a user it is
one activity
• To database:
– Read balance of A : read( A)
– Read balance of B : read (B)
– Subtract 500 birr from A
– Add 500 birr to B
– Write new value of A back to disk
– Write new value of B back to disk
6
Cont’d
• Transaction to transfer 500 birr from account A to
account B:
1.read(A)
2.A := A – 500
3.write(A)
4.read(B)
5.B := B + 500
6.write(B)
7
Transaction states and additional
operations
- For recovery purposes, it keeps track of start,
terminate and commits or aborts
- the recovery manager of the DBMS needs to keep
track of the following operations :
BEGIN_TRANSACTION
BEGIN_TRANSACTION
READ OR WRITE
END_TXN
COMMIT_TXN
ROLLBACK or ABORT
• Failed state need rollback operation
8
Transaction States
• transaction must be in one of several states:
– Active : A transaction that has executed at least one
action, but has not completed.
– Partially committed : A transaction that has
completed all actions, but has not been confirmed as
completed.
– Failed : A transaction that fails completion checks, or
is aborted during the active state.
– Committed : A completed and checked transaction
– Aborted : A transaction that has failed and has had no
effect on the database system
9
Cont’d
10
Desirable Properties of Transactions
• Transactions should possess several properties,
often called the ACID properties;
• The following are the ACID properties
Atomicity
Consistency preservation
Isolation
Durability or permanency
11
Cont’d
• Atomicity
A transaction is an atomic unit of processing;
it should either be performed in its entirety or not
performed at all
• Consistency preservation.
A transaction should be consistency preserving,
it is completely executed from beginning to end
without interference from other transactions,
12
Cont’d
• Isolation
A transaction should appear as though it is being
executed in isolation from other transactions,
even though many transactions are executing
concurrently
• Durability or permanency.
The changes applied to the database by a committed
transaction must persist in the database.
These changes must not be lost because of any failure.
13
Schedules
• Multiple transactions can be executed concurrently
by interleaving their operations
• Schedule S – a sequences of instructions that specify the
chronological order in which instructions of concurrent
transactions T1, T2, … , Tn are executed
– A schedule for a set of transactions must consist of all
instructions of those transactions.
– Must preserve the order in which the instructions appear
in each individual transaction.
14
Cont’d
Serial schedule
S is a schedule that executes all of the operations from
one transaction T1, before moving on to the operations
of another transaction T2.
Non-serial schedule(interleaved schedule )
An is a schedule in which the operations of an
individual transaction are executed in order with
respect to the same transaction,
This is a type of Scheduling where the operations of
multiple transactions are interleaved.
This might lead to a rise in the concurrency problem
15
Serial schedule
16
Non-serial schedules
17
Result Equivalent Schedules
• Two schedules are result equivalent if they
produce the same final state of the database
• Problem: May produce same result by accident!
S1 S2
read_item(X); read_item(X);
X:=X+10; X:=X*1.1;
write_item(X); write_item(X);
Schedules S1 and S2 are result equivalent for X=100 but not in general
18
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.
• Serializable schedule:
– a schedule whose effect on any consistent database
instance is identical to that of some complete serial
schedule over the set of committed transactions in S.
– A nonserial schedule S is serializable is equivalent to say that it is
correct to the result of one of the serial schedule
19
Serializability of Schedules
Serializability
• A schedule S of n transactions is serializable if
it is equivalent to some serial schedule of the
same n transactions
• Kinds of serializability
– Conflict-serializable schedule
– View-serializable schedule
20
Result Equivalent Schedules
• Two schedules are result equivalent if they
produce the same final state of the database
• Problem: May produce same result by accident!
S1 S2
read_item(X); read_item(X);
X:=X+10; X:=X*1.1;
write_item(X); write_item(X);
Schedules S1 and S2 are result equivalent for X=100 but not in general
21
Two types of equivalent schedule:
22
Conflict Serializable
• Schedule S is conflict serializable if it is conflict
equivalent to some serial schedule S’
– We can reorder the non-conflicting operations to
improve efficiency
• Non-conflicting operations:
– Reads and writes from same transaction
– Reads from different transactions
– Reads and writes from different transactions on
different data items
• Conflicting operations:
– Reads and writes from different transactions on
same data item
23
• Two operations in a schedule are side to be conflict if they
satisfy all the 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); Conflict operations
W1(X) and w2(X)
24
Cont’d
Testing for conflict serializability: Algorithm
– Looks at only read_Item (X) & write_Item (X) operations
– Constructs a precedence graph (serialization graph) - a graph
with 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.
25
Fig 1:
26
Constructing the Precedence Graphs
• FIGURE 2: Constructing the precedence graphs for schedules A and D
from Figure 1 to test for conflict serializability.
(a) Precedence graph for serial schedule A.
(b) Precedence graph for serial schedule B.
(c) Precedence graph for schedule C (not serializable).
(d) Precedence graph for schedule D (serializable, equivalent to schedule
A).
27
View Equivalence and View Serializability
28
view equivalence:
• As long as 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 same
view in both schedules.
29
• Two schedules are said to be view equivalent if the following
three conditions hold:
1. The same set of transactions participates in S and S’, and S
and S’ include the same operations of those transactions.
2. If Ti reads a value A written by Tj in S1 , it must also read
the value of A written by Tj in S2
3. for each data object A, the transaction that perform the
final write on x in S1 must also perform the final write on
A in S2
S’ S
30
• Relationship between view and conflict equivalence:
– The two are same under constrained write
assumption which assumes that if T writes X, it is
constrained by the value of X it read;
i.e., new X = f(old X)
– Conflict serializability is stricter than view
serializability.
– a schedule that is view serializable is not necessarily
conflict serializable.
– Any conflict serializable schedule is also view
serializable, but not vice versa.
31
Reading Assignment
Strict schedule
32