Mod 6
Mod 6
Mod 6
Transaction Processing
• A transaction is an action, or series of actions that are being
performed by a single user or application program, which reads or
updates the contents of the database.
• A transaction is a set of logically related operations.
• For example, you are transferring money from your bank account to
your friend’s account, the set of operations would be like this:
• Simple Transaction Example
1. Read your account balance
2. Deduct the amount from your balance
3. Write the remaining balance to your account
4. Read your friend’s account balance
5. Add the amount to his account balance
6. Write the new updated balance to his account
2
Transaction Processing
• Let’s take an example of a simple transaction.
Suppose a bank employee transfers Rs 500
from A's account to B's account. This very
simple and small transaction involves several
low-level tasks.
3
Transaction Processing
4
Transaction states and additional
operations
• 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.
• The recovery manager keeps track of the following operations :
– BEGIN_TRANSACTION
– READ OR WRITE
– COMMIT_TRANSACTION
– ROLLBACK
– END_TRANSACTION
5
Transaction States
IS 533 - Transactions 6
Transaction States
• Active − In this state, the transaction is being
executed. This is the initial state of every
transaction.
• Partially Committed − When a transaction
executes its final operation, it is said to be in a
partially committed state.
• Failed − A transaction is in this state once the
normal execution of the transaction cannot
proceed. A failed transaction can no longer
proceed further.
IS 533 - Transactions 7
Transaction States
• Aborted − If any of the checks fails and the
transaction has reached a failed state, then the
recovery manager rolls back all its write
operations on the database to bring the database
back to its original state where it was prior to the
execution of the transaction. Transactions in this
state are called aborted. The database recovery
module can select one of the two operations
after a transaction aborts −
– Re-start the transaction
– Kill the transaction
IS 533 - Transactions 8
Transaction States
• Committed − If a transaction executes all its
operations successfully, it is said to be
committed. All its effects are now
permanently established on the database
system.
9
Desirable Properties of transactions
11
IS 533 - Transactions 12
Schedules
• A schedule is a sequence of read, write, abort
and commit operations from a set of
transactions.
• Each schedule must preserve the order of the
operations in its constituent transactions.
IS 533 - Transactions 14
Schedules
• Types of Schedules
– Serial Schedules
– Parallel Schedules
Schedules
• Types of Schedules
– Serial Schedules
In a serial schedule, at any point of time,
only one transaction is active, i.e. there is no
overlapping of transactions.
Schedules
• Types of Schedules
– Non-serial or Parallel/ concurrent Schedules
In parallel schedules, more than one
transactions are active simultaneously, i.e. the
transactions contain operations that overlap at
time.
Characterizing Schedules based on
Recoverability
• Type of schedules :
– Recoverable : If T2 is reading value updated by T1
and commit of T2 is delayed till commit of T1 , the
schedule is called recoverable.
18
IS 533 - Transactions 19
Schedules and Conflicts
• Conflict in Schedules
– In a schedule comprising of multiple transactions,
a conflict occurs when two active transactions
perform non-compatible operations. Two
operations are said to be in conflict, when all of
the following three conditions exists
simultaneously −
• The two operations are parts of different transactions.
• Both the operations access the same data item.
• At least one of the operations is a write_item()
operation, i.e. it tries to modify the data item.
Read and Write Operation Conflict
Rules
Operations of different Conflict Reason
transactions
read read No Because the effect of a pair of read operations
does not depend on the order in which they are
executed
read write Yes Because the effect of a read and a write operation
depends on the order of their execution
write write Yes Because the effect of a pair of write operations
depends on the order of their execution
Conflicting operations may give rise
to the following anomalies:
Flat transaction
Consists of a sequence of primitive operations embraced
between a begin and end markers.
Begin_transaction Reservation
…
end.
Nested transaction
The operations of a transaction may themselves be
transactions.
Begin_transaction Reservation
…
Begin_transaction Airline
…
end. {Airline}
Begin_transaction Hotel
…
end. {Hotel}
end. {Reservation}
Example:
In concurrent execution environment if T1 conflicts with T2
over a data item A, then the existing concurrency control
decides if T1 or T2 should get the A and if the other
transaction is rolled-back or waits.
Y N
Write
N N
one at a time.
Unlocking (Shrinking) Phase:
A transaction unlocks its locked data items one at a time.
Requirement:
For a transaction these two phases must be mutually exclusively,
that is, during locking phase unlocking phase must not start and
during unlocking phase locking phase must not begin.
T1 T2 Result
read_lock (Y); X=50; Y=50
read_item (Y); Nonserializable because it.
unlock (Y); violated two-phase policy.
read_lock (X);
read_item (X);
unlock (X);
Time write_lock (Y);
read_item (Y);
Y:=X+Y;
write_item (Y);
unlock (Y);
write_lock (X);
read_item (X);
X:=X+Y;
write_item (X);
unlock (X);
T1 obey 2PL
T2 obey 2PL
T3 not obey 2PL
T’1 T’2
read_lock (Y); read_lock (X); T1 and T2 follow two-phase
read_item (Y); read_item (X); policy but they are subject to
write_lock (X); Write_lock (Y); deadlock, which must be
unlock (Y); unlock (X); dealt with.
read_item (X); read_item (Y);
X:=X+Y; Y:=X+Y;
write_item (X); write_item (Y);
unlock (X); unlock (Y);
• A transaction is said to follow the two-phase locking protocol if all locking operations (read_lock,
write_lock) precede the first unlock operation in the transaction.
• Each transaction should have two phases: (a) Locking (Growing) phase, and (b) Unlocking (Shrinking)
Phase.
Locking (Growing) Phase: A transaction applies locks (read or write) on desired data items one at a time.
Can also try to upgrade a lock.
Unlocking (Shrinking) Phase: A transaction unlocks its locked data items one at a time. Can also
downgrade a lock.
• Requirement: For a transaction these two phases must be mutually exclusively, that is, during locking
phase no unlocking or downgrading of locks can occur, and during unlocking phase no new locking or
upgrading operations are allowed.
Basic Two Phase Locking -
• When transaction starts executing, it is in the locking phase, and it can request locks on new items or
upgrade locks. A transaction may be blocked (forced to wait) if a lock request is not granted. This may lead
to several transactions being in a state of deadlock.
Once the transaction unlocks an item (or downgrades a lock), it starts its shrinking phase and can no
longer upgrade locks or request new locks.
The combination of locking rules and 2-phase rule ensures serializable schedules.
Theorem: If every transaction in a schedule follows the 2PL rules, the schedule must be serializable.
(Proof is by contradiction.)
Drawbacks of Two Phase Locking Protocol
• Conservative 2PL: A transaction must lock all its items before starting execution. If any item it needs is
not available, it locks no items and tries again later. Conservative 2PL has no deadlocks since no lock
requests are issued once transaction execution starts.
• Strict 2PL: All items that are exclusive locked by a transaction are not released (unlocked) until after the
transaction commits. This is the most commonly used two-phase locking algorithm, and ensures strict
schedules (for recoverability and cascadless rollbacks).
• Rigorous 2PL: All items that are shared locked or exclusive locked by a transaction are not released
(unlocked) until after the transaction commits. Also guarantees strict schedules.
Distributed Transactions
• A distributed transaction is a set of operations on data that is performed across two or more data
repositories (especially databases). It is typically coordinated across separate nodes connected by a network,
but may also span multiple databases on a single server.
• Just like centralized transactions, distributed transactions also support ACID properties :
A - Atomicity C - consistency I - isolation D - Durability
• Why do we need Distributed Transactions? Some of the ACID properties are harder to implement in
centralized transaction system, AND basic single - system Techniques( in centralized system ) are always
not sufficient.
If client runs transaction then each transaction must complete
before proceedding to the next.
If transactions are nested then transactions at the same level
can run in parallel
clients use a single server to act as co-ordinator for all other
transactions. The co-ordinator handles all communication
with other servers.
• This commit protocol is inadequate because when the client requests a commit, it does not allow a server to
make a unilateral decision to abort a transaction, thus violating the Durability property of ACID. E.g.
deadlock avoidance may force a transaction to abort at a server when locking is used. So any server may
fail or abort and client is not aware.
Distributed Two Phase Commit Protocol
canCommit?(trans)-> Yes / No
Call from coordinator to participant to ask whether it can commit a transaction. Participant replies with its vote.
doCommit(trans)
Call from coordinator to participant to tell participant to commit its part of a transaction.
doAbort(trans)
Call from coordinator to participant to tell participant to abort its part of a transaction.
haveCommitted(trans, participant)
Call from participant to coordinator to confirm that it has committed the transaction.
• Such a participant is uncertain and cannot proceed any further. The objects used by its transaction cannot
be released for use by other transactions.
• Participant makes a getDecision request to the coordinator to determine the outcome. If the coordinator has
failed, the participant will not get the decision until the coordinator is replaced resulting in extensive delay
for participant in uncertain state.
• Timeout are used since exchange of information can fail when one of the servers crashes, or when
messages are lost So process will not block forever.
Performance of Two Phase Commit Protocol
• Provided that all servers and communication channels do not fail, with N participants
• N number of canCommit? Messages and replies
• Followed by N doCommit messages
• The cost in messages is proportional to 3N
• The cost in time is three rounds of message.
• The cost of haveCommitted messages are not counted, which can function correctly without them- their
role is to enable server to delete stale coordinator information
Failure of Coordinator
• When a participant has voted Yes and is waiting for the coordinator to report on the outcome of the vote,
such participant is in uncertain stage. If the coordinator has failed, the participant will not be able to get the
decision until the coordinator is replaced, which can result in extensive delays for participants in the
uncertain state.
• One alternative strategy is allow the participants to obtain a decision from other participants instead of
contacting coordinator. However, if all participants are in the uncertain state, they will not get a decision.
Distributed Transaction
Topics in Distributed Transactions
Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn.4
© Pearson Education 2005
Figure 14.1
Distributed transactions
X
Client T1 N
T T
Y 12
T
T
T
21
T2
Client
Y
P
Z
T
22
Flat transaction send out requests to different servers and each request is
completed before client goes to the next one. Nested transaction allows sub-
transactions at the same level to execute concurrently.
Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn.4
© Pearson Education 2005
Figure 14.2
Nested banking transaction
X
Client T A a.withdraw(10)
1
T
T = openTransaction Y
openSubTransaction T B b.withdraw(20)
2
a.withdraw(10);
openSubTransaction
b.withdraw(20); Z
openSubTransaction
c.deposit(10); T
3 C c.deposit(10)
openSubTransaction
d.deposit(20); T D d.deposit(20)
4
closeTransaction
Instructor’s
Instructor’s
GuideGuide
for Coulouris,
for Coulouris,
Dollimore
Dollimore
and Kindberg
and Kindberg
Distributed
Distributed
Systems:
Systems:
Concepts
Concepts
and Design
and Design
Edn.4Edn.4
© Pearson
© Pearson
Education
Education
20052005
Coordinator of a distributed transaction
T = openTransaction
join BranchY
a.withdraw(4);
c.deposit(4); participant
b.withdraw(3);
d.deposit(3); C c.deposit(4);
closeTransaction
D d.deposit(3);
Note: client invoke an operation b.withdraw(),
B will inform participant at BranchY to join coordinator. BranchZ
Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn.4
© Pearson Education 2005
Two-phase commit protocol
Allow any participant to abort its part of a transaction. Due to atomicity, the
whole transaction must also be aborted.
In the first phase, each participant votes for the transaction to be committed
or aborted. Once voted to commit, not allowed to abort it. So before votes to
commit, it must ensure that it will eventually be able to carry out its part,
even if it fails and is replaced.
A participant is said to be in a prepared state if it will eventually be able to
commit it. So each participant needs to save the altered objects in the
permanent storage device together with its status-prepared.
Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn.4
© Pearson Education 2005
Two-phase commit protocol
Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn.4
© Pearson Education 2005
Two-phase commit protocol
Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn.4
© Pearson Education 2005
Figure 14.4
Operations for two-phase commit protocol
canCommit?(trans)-> Yes / No
Call from coordinator to participant to ask whether it can commit a
transaction. Participant replies with its vote.
doCommit(trans)
Call from coordinator to participant to tell participant to commit its part of a
transaction.
doAbort(trans)
Call from coordinator to participant to tell participant to abort its part of a
transaction.
haveCommitted(trans, participant)
Call from participant to coordinator to confirm that it has committed the
transaction.
getDecision(trans) -> Yes / No
Call from participant to coordinator to ask for the decision on a transaction
after it has voted Yes but has still had no reply after some delay. Used to
recover from server crash or delayed messages.
Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn.4
© Pearson Education 2005
Figure 14.5
The two-phase commit protocol
Coordinator Participant
Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn.4
© Pearson Education 2005
two-phase commit protocol
Consider when a participant has voted Yes and is waiting for the coordinator to report on the
outcome of the vote by telling it to commit or abort.
Such a participant is uncertain and cannot proceed any further. The objects used by its
transaction cannot be released for use by other transactions.
Participant makes a getDecision request to the coordinator to determine the outcome. If the
coordinator has failed, the participant will not get the decision until the coordinator is
replaced resulting in extensive delay for participant in uncertain state.
Timeout are used since exchange of information can fail when one of the servers crashes,
or when messages are lost So process will not block forever.
Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn.4
© Pearson Education 2005
Performance of two-phase commit protocol
Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn.4
© Pearson Education 2005
Failure of Coordinator