You are on page 1of 53

Transactions and Concurrency Control

Presented By:
Tsitsi Mandizvidza
Reg number c14122839t
1.0
What is A Transaction

 A transaction is a point where there is


interaction with the database

 A transaction is generally atomic

 The state of the transaction being done is


not visible. If it is not done completely, any
changes it made will be undone. This is
known as rollback
Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
Transactions primitives

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
1.1
Properties of transactions : ACID

Atomic – The transaction happens as a single indivisible action. Others do


not see intermediate results. All or nothing.
• Consistent – If the system has invariants, they must hold after the
transaction. Example, total amount of money in all accounts must be the same
before and after a “transfer funds” transaction.
• Isolated (Serializable) – If transactions run at the same time, the final
result must be the same as if they executed in some serial order.
 • Durable – Once a transaction commits, the results are made permanent.
No failures after a commit will cause the results to revert.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
1.2
Atomic Transactions

Transaction: an operation composed of a number of


discrete steps.
All the steps must be completed for the transaction to be
committed.
The results are made permanent.
 Otherwise, the transaction is aborted and the state of
the system reverts to what it was before the transaction
started.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
1.3
Atomic Transactions

 The transaction is executed as a single unit of work .


 That is ,for every transaction ,either all actions within
transaction are carried out or none are
 A transaction might commit after completing all its
actions ,or it could abort(or aborted by the
DBMS)after executing some actions .
 In a committed transaction all actions are executed
within a DBMS and an aborted transaction none of
the actions are executed .
 DBMS logs actions so that it can undo the actions of
aborted transactions
Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
1.3
Example – Atomic Transaction

Example Buying a house:


Make an offer
Sign contract
Deposit money
Inspect the house – Critical problems from inspection?
Get a mortgage
Have seller make repairs
 Commit: sign closing papers & transfer deed Or Abort: return deposited money
and revert to pre-purchase state
All or nothing property

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
ISOLATION TRANSACTION

 Due to performance reasons, actions of multiple


transactions are interleaved
 The isolation property ensures that even though
actions of several transactions are interleaved ,the
net effect is identical to executing all transactions
one after the other in some serial order .
Example of isolation

 T and R accesses withdraws funds from the joint


account simultaneously from different locations .
 T reads balance ($1000)
 R reads balance ($1000)
 T withdraws $500(new balance $500)
 R withdraws $700(new balance $300)
 T’s transaction writes new balance
 R’s transaction write new balance
 Does not preserve isolation property
Locks are therefore needed
DURABILITY OF TRANSACTIONS

 This property ensures that transaction survive


system crashes and failures
 That is ,a committed transaction is always reflected
in the database and an uncommitted transaction is
always rolled backed after a system crash
Transaction in dbms

 They should be used when performing multiple


operations against one or more tables to allow you
to return the database to a consistent state in case
there is an error in one of the operations.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
Nested transaction

 A nested transaction is a
database transaction that is started by an
instruction within the scope of an already
started transaction.
 Nested transactions are implemented differently in
different databases.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
Nested transactions

 A top-level transaction may create sub


transactions
 Problem: – sub transactions may commit (results
are durable) but the parent transaction may
abort.
 One solution: private workspace – Each sub
transaction is given a private copy of every object
it manipulates.
 On commit, the private copy displaces the
parent’s copy (which may also be a private copy
of the parent’s parent)

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
THE RULES FOR COMMITTING OF NESTED TRANSACTIONS

 A transaction commits or aborts only after its child


transactions have completed
 When a sub-transaction completes, it makes an
independent decision on provisionally commit or
abort. Its decision to abort is final.
 When a parent aborts, all of its sub-transactions are
aborted, even though some of them may have
provisionally committed.
 When a sub-transaction aborts, the parent can
decide whether to abort or not.
Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
CONTINUATION

 When the top-level transaction commits, then all of


the sub-transactions that have provisionally
committed can commit.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
Example
USEFULNESS OF NESTED TRANSACTION

 May run concurrently with other nested


transaction at the same level in the hierarchy.
 This will allow additional concurrency in a
transaction.
 Concurrency control is used to isolate the effects
of concurrent nested transactions from one
another.
Continuation

 Nested transactions can commit or abort independently


 Comparing the single transaction ,a nested transaction
is more robust.
 The aborting of a nested transaction does not
necessarily imply its parent must abort.
 For example considering transfer transaction , when
the two transaction (T1 and T2)both commit the
transfer transaction can also commit .
TWO IMPORTANCE OF NESTED TRANSACTIONS

 Additional concurrency in a transaction


Sub transactions at one level may run concurrently
with other sub-transactions at the same level in the
hierarchy.
 More robust
Sub-transactions can commit or abort independently.
For example, a transaction to deliver a mail
message to a list of recipients.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
Concurrency control

 Concurrency – multiple users want to access the


same data at the same time
 Concurrency control (CC) ensures that correct
results for parallel operations are generated
 CC provides rules, methods, design methodologies
and theories to maintain the consistency of
components operating simultaneously while
interacting with the same object
Why Concurrency control?

 Concurrency control is needed because there are a


lot of things that can go wrong

 Each transaction itself can be okay, but the


concurrency generates problems such as:
 The lost update problem
 The dirty read problem
 The incorrect summary problem
THE LOST UPDATE PROBLEM

 Example
 A and B, might both read the same row and
calculate new values for one of the columns based
on the data that these applications read. If A
updates the row and then B also updates the row,
A's update lost.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
continuation

THE DIRTY READ PROBLEM


This effect happens when a transaction reads the
value of an object x who is not stable because the
other transaction is still running so, for atomicity
property, there will be a rollback cascade.
The incorrect summary problem
This problem is caused when one of the transactions
is executing an aggregate operation on several data
items, and other transactions are updating one or more
of those data items. This causes a inconsistent
database state.
Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
Read and write conflicts

Figur 13.9
Three methods

 There are many methods for concurrency control

 The main methods are:


 Locks
 Optimistic concurrency control
 Timestamp ordering
Locks

 Locks are used to order transactions that access the


same objects according to the order of arrival of their
operations at the objects
 For example transaction T2 does not have access to a
data item that is currently being used by transaction T1

 Mechanism for enforcing limits on access to the object

 If the access to the object is locked other users have to


wait until it’s unlocked
Lock compability
LOCK TYPES

Binary Locks
 Have only two states: locked (1) or unlocked (0).
Shared/Exclusive Locks
exists when access is reserved specifically for the
transaction that locked the object .
A shared lock exists when concurrent transactions
are granted read access on the basis of common lock
shared lock produces no conflict as long as all the
concurrent transactions are read only
LOCK TYPES

DEADLOCKS
A deadlock occurs when two transactions wait
indefinitely for each other to unlock data.
Deadlock detection. The DBMS periodically tests the
database for deadlocks. if a deadlock is found one of
the transactions is aborted (rolled back and restarted)
and the other transaction are continues.
A problem with lock

 The use of locks can lead to deadlocks

 Ex: Two transactions are waiting and each is


dependent on the other to release a lock so it can
resume

Held by Waits for


A
T U T U

Waits for B
Held by
Drawbacks of locks

 Lock maintenance represent an overhead that is


not present in systems that do not support
concurrent to shared data.
 Use of locks can result to deadlock. Deadlocks
prevention reduces concurrency severely and
therefore deadlock situation must be resolved by the
issue of timeouts or by deadlock detection.
Example
Lock class

public class Lock {


private Object object; // the object being protected by the lock
private Vector holders; // the TransIDs of current holders
private LockType lockType; // the current type
public synchronized void acquire(TransID trans, LockType LockType ){
while(/*another transaction holds the lock in conflicing mode*/) {
try {
wait();
}catch ( Interrupted Exception e){/*...*/ }
}
if(holders.isEmpty()) { // no TIDs hold lock
holders.addElement(trans);
lockType = aLockType;
} else if(/*another transaction holds the lock, share it*/ ) ){
if(/* this transaction not a holder*/) holders.addElement(trans);
} else if (/* this transaction is a holder but needs a more exclusive lock*/)
lockType.promote();
}
}
Continues on next slide
Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
Example
continued

public synchronized void release(TransID trans ){


holders.removeElement(trans); // remove this holder
// set locktype to none
notifyAll();
}
}

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
Optimistic concurrency control

 Introduced by Kung and Robison(1981)

 Optimistic concurrency control (OCC) is based on


the assumption that two transactions won’t occur at
the same time

 OCC is generally used in environments with low data


contention
OCC phases

 Begin

 Working phase

 Validation phase

 Update phase
OCC phases

 Begin:
Record a timestamp marking the transaction's
beginning

 Working phase:
Read and write database values to a copy of the
transaction which is not yet bound to the object.
Several different transactions of this type may
coexist
OCC phases

 Validate phase:
Controls if the transaction conflicts with another
transaction on the same object

 Update phase:
 Committed: If there is no conflict, make all changes part of
the official state of the database
 Aborted: If there is a conflict, resolve it, typically by
aborting the transaction, although other resolution
schemes are possible
Timestamp

 Whenever a transaction starts, it is given a


timestamp
 The timestamp tells in which order that the
transactions are supposed to be applied in
 The transaction with the earliest timestamp is
executed first
 Every object in the database has a read timestamp
and a write timestamp
Major goals

 Serializability

 Recoverability

 Distribution
Serializability

 The goal with serializability is that the result of the


parallel transactions is the same as if though the
transactions were preformed serially
Recoverability

 Recoverability means that committed transactions


do not read data written by aborted transactions
distribution

 Distributed serializability

 The serializability concept Commitment Ordering


allows to effectively achieve global serializability
across multiple Database Systems, that may use
different concurrency control mechanisms.
Comparison of concurrency control method

PROPERTIES CHARACTORISTICS APPLICATIONS STRENGTHS WEAKNESS

all access by a transaction • File save • used to order • Lock maintenance


locks to a particular object must • database transactions that represent an overhead
be serialized with respect system access the same that is not present in
to another transaction’s objects according to systems that do not
access the order of arrival of support concurrent to
their operations at the shared data.
objects

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
continuation

OPTIMISTIC CHARACTORISTICS APPLICATIONS STRENGTHS WEAKNESS

OPTIMISTIC • based on the • Only suitable for • This technique is very • Conflicts are
assumption that environments efficient when conflicts expensive to
two transactions where there are are rare. The deal with, since
won’t occur at few conflicts and occasional conflicts the conflicting
the same time no long result in the transaction transaction must
transactions. roll back. be rolled back.
• Acceptable for • Longer
mostly Read or • The rollback involves transactions are
Query database only the local copy of more likely to
systems that data, the database is have conflicts
require very few not involved and thus and may be
update there will not be any repeatedly
transactions cascading rollbacks. rolled
• For document back because of
sharing over the conflicts with
internet short
transactions.
continuation

PROPERTIES CHARACTORISTICS APPLICATIONS STRENGTHS WEAKNESS

TIMESTAMP ODERING • Each operation • Used in file • The timestamp • The timestamp
in a transaction servers. ordering based protocol
is validated • Database protocol ensures may produce
when it is system conflict schedule which
carried out serializability. is not
This is because recoverable.
conflicting
operations are
processed in
timestamp order.

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
References
Questions

1 What is concurrency control, and what is its objective?

2. What is a lock, and how, in general, does it work?


3. With the aid of a diagram, describe the cause of a deadlock. How can a deadlock
be avoided? Discuss several deadlock-avoidance strategies.
ANSWERS

ANSWER TO QUESTION 1
ANSWER TO QUESTION 2
ANSWER TO QUESTION 3

Although locks prevent serious data inconsistencies, their use may lead to two major
problems:
1.The transaction schedule dictated by the locking requirements may not be
serializable, thus causing data integrity and consistency problems.
2.The schedule may create deadlocks.
Deadlocks are the equivalent of a traffic gridlock in a big city and are caused by two
transactions INDEFINITELY waiting for each other to unlock data.

In the following table (Next Slide), we can illustrate the scenario that
leads to a deadlock.)
Illustration of a deadlock

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
Three basic techniques exist to control deadlocks:

Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000

You might also like