You are on page 1of 17

Objectives

‹ Function and importance of transactions.


‹ Properties of transactions.
‹ Concurrency Control
– Meaning of serializability.
serializability
– How locking can ensure serializability.
Transaction Management
– Deadlock and how it can be resolved.
– How timestamping can ensure serializability.
– Optimistic concurrency control.
– Granularity of locking.
2
1

Objectives Transaction Support


‹ Recovery Control Transaction
– Some causes of database failure. Action, or series of actions, carried out by user or
application, which reads or updates contents of
– Purpose of transaction log file. database.
– Purpose of checkpointing.
checkpointing ‹ Logical
L i l unit
it off work
k on the
th database.
d t b
– How to recover following database failure. ‹ Application program is series of transactions with non-
‹ Alternative models for long duration transactions. database processing in between.
‹ Transforms database from one consistent state to
another, although consistency may be violated while
transaction is in progress.

3 4

Example Transaction Transaction Support


‹ Can have one of two outcomes:
– Success - transaction commits and database reaches a
new consistent state.
– Failure - transaction aborts, and database must be
restored to consistent state before it started.
started
– Such a transaction is rolled back or undone.
‹ Committed transaction cannot be aborted.
‹ Aborted transaction that is rolled back can be
restarted later.

5 6
State Transition Diagram for Transaction Properties of Transactions
‹Four basic (ACID) properties of a transaction are:

Atomicity ‘All or nothing’ property.


Consistency Must transform database from one consistent
state to another.
Isolation Partial effects of incomplete transactions
should not be visible to other transactions.
Durability Effects of a committed transaction are
permanent and must not be lost because of later failure.

7 8

DBMS Transaction Subsystem Concurrency Control


Process of managing simultaneous operations on
the database without having them interfere with
one another.
‹ Prevents interference when two or more users
are accessing database simultaneously and at
least one is updating data.
‹ Although two transactions may be correct in
themselves, interleaving of operations may
produce an incorrect result.

9 10

Need for Concurrency Control Lost Update Problem


‹ Three examples of potential problems caused by ‹ Successfully completed update is overridden by
concurrency: another user.
– Lost update problem. ‹ T1 withdrawing £10 from an account with balx,
– Uncommitted dependency
p ypproblem. initially £100.
– Inconsistent analysis problem. ‹ T2 depositing £100 into same account.
‹ Serially, final balance would be £190.

11 12
Lost Update Problem Uncommitted Dependency Problem
‹ Occurs when one transaction can see
intermediate results of another transaction
before it has committed.
‹ T4 updates balx to £200 but it aborts, so balx
should be back at original value of £100.
‹ T3 has read new value of balx (£200) and uses
value as basis of £10 reduction, giving a new
‹ Loss of T2’s update avoided by preventing T1 balance of £190, instead of £90.
from reading balx until after update.

13 14

Uncommitted Dependency Problem Inconsistent Analysis Problem


‹ Occurs when transaction reads several values
but second transaction updates some of them
during execution of first.
‹ Sometimes referred to as dirty read or
unrepeatable read.
‹ T6 is totaling balances of account x (£100),
account y (£50), and account z (£25).
‹ Problem avoided by preventing T3 from ‹ Meantime, T5 has transferred £10 from balx to
reading balx until after T4 commits or aborts. balz, so T6 now has wrong result (£10 too high).

15 16

Inconsistent Analysis Problem


Serializability
‹ Objective of a concurrency control protocol is to
schedule transactions in such a way as to avoid any
interference.
‹ One obvious solution is to allow only one transaction to
execute at a time
‹ However, the aim of a multi-user DBMS is to maximize
parallelism in the system
– E.g. transactions that access different parts of DB can
be scheduled together without interference
‹ Could run transactions serially, but this limits degree of
‹ Problem avoided by preventing T6 from reading concurrency or parallelism in system.
balx and balz until after T5 completed updates. ‹ Serializability identifies those executions of transactions
guaranteed to ensure consistency.
18
Serializability Serializability
Schedule
Sequence of reads/writes by set of concurrent ‹ In serial schedule, transactions are performed in
transactions. serial order
Serial Schedule – ( T1 => T2 or T2 => T1 )
Schedule where operations of each transaction – There is
i no interference
i f between transactions
i
are executed consecutively without any ‹ No guarantee that results of all serial executions of
interleaved operations from other transactions. a given set of transactions will be identical.
– E.g. In banking, it matters whether interest is calculated
on an account before a large deposit is made or after

19 20

Nonserial Schedule Serializability


‹ A Schedule where the operations from set of ‹ In serializability, ordering of read/writes is
concurrent transactions are interleaved. important:
‹ Objective of serializability is to find nonserial (a) If two transactions only read a data item, they
schedules that allow transactions to execute do not conflict and order is not important.
concurrently
l without
ih i
interfering
f i with
i h one (b) If two
t transactions
t ti either
ith read d or write
it
another. completely separate data items, they do not
conflict and order is not important.
‹ In other words, want to find nonserial schedules
that are equivalent to some serial schedule. Such (c) If one transaction writes a data item and
a schedule is called serializable. another reads or writes same data item, order
of execution is important.

21 22

Example of Conflict Serializability


Serializability
‹ Conflict serializable schedule orders any
conflicting operations in same way as some serial
execution.
‹ Under constrained write rule (transaction
updates data item based on its old value, which is
Doesn’t Conflict
first read), use precedence graph to test for
=> Order can
change
serializability.

24
Precedence Graph Example - Non-conflict serializable schedule
‹ Create: ‹ T9 is transferring £100 from one account with
– node for each transaction; balance balx to another account with balance
– a directed edge Ti → Tj, if Tj reads the value baly.
of an item written byy Ti; ‹ T10 is increasing balance of these two accounts by

– a directed edge Ti → Tj, if Tj writes a value 10%.


into an item after it has been read/written by
Ti.
‹ If precedence graph contains cycle schedule is
not conflict serializable.

25 26

Example - Non-conflict serializable schedule Example - Non-conflict serializable schedule


‹ Precedence graph has a cycle and so is not
serializable.

27 28

Recoverability Example - Recoverability


‹ Serializability identifies schedules that maintain
‹ What if at t14
database consistency, assuming no transaction
fails. T9 aborts?
‹ Could also examine recoverability of ‹ T10 used balx
transactions within schedule.
schedule written by T9
‹ If transaction fails, atomicity requires effects of and committed
transaction to be undone. ‹ Recoverable
‹ Durability states that once transaction commits, schedule or
its changes cannot be undone (without running not?
another, compensating, transaction).

29 30
Recoverable Schedule Concurrency Control Techniques
A schedule where, for each pair of transactions ‹ Two basic concurrency control techniques:
Ti and Tj, if Tj reads a data item previously – Locking,
written by Ti, then the commit operation of Ti – Timestamping.
precedes the commit operation of Tj.
‹ Both are conservative approaches: delay
Ti → Tj transactions in case they conflict with other
transactions.
‹ Optimistic methods assume conflict is rare and
only check for conflicts at commit.

31 32

Concurrency Control Techniques:


1-Locking Locking - Basic Rules
Transaction uses locks to deny access to other ‹ If transaction has shared lock on item, can read
transactions and so prevent incorrect updates. but not update item.
‹ If transaction has exclusive lock on item, can both
‹ Most widely used approach to ensure
serializability. read and update item.
‹ Reads
R d cannott conflict, fli t so more than th one
‹ Generally, a transaction must claim a shared
(read) or exclusive (write) lock on a data item transaction can hold shared locks simultaneously
before read or write. on same item.
‹ Exclusive lock gives transaction exclusive access
‹ Lock prevents another transaction from
modifying item or even reading it, in the case of a to that item.
write lock.
33 34

Locking - Basic Rules Example - Incorrect Locking Schedule


‹ Some systems allow transaction to upgrade read balx baly
lock to an exclusive lock, or downgrade exclusive 100 400

lock to a shared lock.

35 36
Example - Incorrect Locking Schedule Example - Incorrect Locking Schedule
‹ For two transactions in previous slide, a valid ‹ If at start, balx = 100, baly = 400, result should be:
schedule using these rules is:
– balx = 220, baly = 330, if T9 executes before T10,
S = {write_lock(T9, balx), read(T9, balx), write(T9, balx), or
unlock(T9, balx),
) write_lock(T
write lock(T10, balx),) read(T10, balx),
) – bal
b lx = 210,
210 bal
b ly = 340,
340 if T10 executes before
b f T9.
write(T10, balx), unlock(T10, balx), write_lock(T10,
baly), read(T10, baly), write(T10, baly), unlock(T10, ‹ However, result gives balx = 220 and baly = 340.
baly), commit(T10), write_lock(T9, baly), read(T9,
baly), write(T9, baly), unlock(T9, baly), commit(T9) } ‹ S is not a serializable schedule.

37 38

Example - Incorrect Locking Schedule Two-Phase Locking (2PL)


‹ Problem is that transactions release locks too Transaction follows 2PL protocol if all locking
soon( E.g. releases lock on balx and obtains lock operations precede first unlock operation in
baly). the transaction.
– This seems to allow greater concurrency but it
permits transactions to interfere with one ‹ Two phases
T h f transaction:
for t ti
another – Growing phase - acquires all locks but
– resulting in loss of total isolation and atomicity cannot release any locks.
‹ To guarantee serializability, need an additional – Shrinking phase - releases locks but cannot
protocol concerning the positioning of lock and acquire any new locks.
unlock operations in every transaction.
39 40

Preventing Uncommitted Dependency Problem


Preventing Lost Update Problem using 2PL using 2PL

41 42
Preventing Inconsistent Analysis Problem using
2PL Cascading Rollback
‹ If every transaction in a schedule follows 2PL,
schedule is serializable.
‹ However, problems can occur with
interpretation of when locks can be released.

43 44

Cascading Rollback Cascading Rollback


‹ Transactions conform to 2PL.
‹ T14 aborts.
‹ Since T15 is dependent on T14, T15 must also be rolled
back. Since T16 is dependent on T15, it too must be rolled
back
back.
‹ This is called cascading rollback.
‹ To prevent this with 2PL, leave release of all locks until
end of transaction.
‹ Another problem: all locking based schemes can cause
deadlock

45 46

Deadlock Deadlock
‹ A situation in which no progress can be made or no
advancement is possible ‹ Only one way to break deadlock: abort one or
‹ An impasse that may result when two (or more) more of the transactions.
transactions are each waiting for locks held by the other to ‹ Deadlock should be transparent to user, so
be released. DBMS should restart transaction(s).
‹ Three general techniques for handling deadlock:
– Timeouts.
– Deadlock prevention.
– Deadlock detection and recovery.

47 48
Timeouts Deadlock Prevention
‹ Transaction that requests lock will only wait for a ‹ DBMS looks ahead to see if transaction would
system-defined period of time. cause deadlock and never allows deadlock to
‹ If lock has not been granted within this period, occur.
lock request times out. ‹ Could order transactions using transaction
‹ In this case, DBMS assumes transaction may be timestamps:
deadlocked, even though it may not be, and it – Wait-Die - only an older transaction can wait
aborts and automatically restarts the transaction. for younger one, otherwise transaction is
aborted (dies) and restarted with same
timestamp.

49 50

Deadlock Prevention Deadlock Detection and Recovery


– Wound-Wait - only a younger transaction can ‹ DBMS allows deadlock to occur but recognizes it
wait for an older one. If older transaction and breaks it.
requests lock held by younger one, younger one ‹ Usually handled by construction of wait-for
is aborted (wounded). graph (WFG) showing transaction dependencies:
– Create a node for each transaction.
transaction
– Create edge Ti -> Tj, if Ti waiting to lock item locked
by Tj.
‹ Deadlock exists if and only if WFG contains
cycle.
‹ WFG is created at regular intervals.

51 52

Example - Wait-For-Graph (WFG) Recovery from Deadlock Detection


‹ Several issues:
– choice of deadlock victim;
– how far to roll a transaction back;
– avoiding starvation.
starvation
» Starvation occurs when same transaction is always chosen as
the victim and the transaction can never complete

53 54
Concurrency Control Techniques:
2-Timestamping Timestamping
‹ Transactions ordered globally so that older Timestamp
transactions, transactions with smaller A unique identifier created by DBMS that
timestamps, get priority in the event of conflict. indicates relative starting time of a
‹ Conflict is resolved by rolling back and transaction.
restarting transaction. ‹ Can be generated by using system clock at time
‹ No locks so no deadlock. transaction started, or by incrementing a logical
counter every time a new transaction starts.

55 56

Timestamping Timestamping - Transaction T issues a Read(x)


‹ Read/write proceeds only if last update on that ‹ Consider a transaction T with timestamp ts(T):
data item was carried out by an older transaction.
‹ Otherwise, transaction requesting read/write is
restarted and given a new timestamp. ts(T) < write_timestamp(x)
‹ Also
Al timestamps
ti t f data
for d t items:
it – x already updated by younger (later) transaction.
– read-timestamp - timestamp of last transaction
to read item; – Transaction must be aborted and restarted with a
new timestamp.
– write-timestamp - timestamp of last
transaction to write item. ts(T) >= write_timestamp(x)
– Read operation can proceed
– Set read_timestamp(x) = max ( ts(T), read_timestamp(x) )
57 58

Timestamping – Transaction T issues a Write(x) Timestamping - Transaction T issues a Write(x)


ts(T) < read_timestamp(x) ts(T) < write_timestamp(x)

– x already read by younger transaction. – x already written by younger transaction.


– T is attempting to write an obsolete value of x
– Roll back transaction T and restart it using a – Roll back transaction T and restart it using a
later timestamp. later timestamp

‹ Otherwise, operation is accepted and executed.

59 60
Example – Basic Timestamp Ordering
Modification: Thomas’s write rule
Timestamping - Transaction T issues a Write(x)

ts(T) < write_timestamp(x)

– x already written by younger transaction.


– Write can safely be ignored - ignore obsolete
write rule. Aborted &
restarted
– Allows greater concurrency

Ignored
--ignore obsolete
write rule

61 62

Optimistic Techniques Optimistic Techniques


‹ Based on assumption that conflict is rare and ‹ Three phases:
more efficient to let transactions proceed without
delays to ensure serializability. – Read
‹ At commit, check is made to determine whether – Validation
conflict has occurred.
occurred – Write
W it
‹ If there is a conflict, transaction must be rolled
back and restarted.
‹ Potentially allows greater concurrency than
traditional protocols.

63 64

Optimistic Techniques - Read Phase Optimistic Techniques - Validation Phase


‹ Extends from start until immediately before ‹ Follows the read phase.
commit. ‹ For read-only transaction, checks that data read
are still current values. If no interference,
‹ Transaction reads values from database and transaction is committed, else aborted and
stores
t th
them i local
in l l variables.
i bl U d t
Updates are restarted.
applied to a local copy of the data.
‹ For update transaction, checks transaction
leaves database in a consistent state, with
serializability maintained.

65 66
Optimistic Techniques - Write Phase Granularity of Data Items
‹ Follows successful validation phase for update ‹ Size of data items chosen as unit of protection by
transactions. concurrency control protocol.
‹ Ranging from coarse to fine:
‹ Updates made to local copy are applied to the
– The entire database.
d t b
database.
– A file.
– A page (or area or database spaced).
– A record.
– A field value of a record.

67 68

Granularity of Data Items Hierarchy of Granularity


‹ Tradeoff: ‹ Could represent granularity of locks in a
– coarser, the lower the degree of concurrency; hierarchical structure.
‹ Root node represents entire database, level 1s
– finer, more locking information that is needed represent files, etc.
to be stored.
‹ When
Wh node d isi locked,
l k d all
ll its
it descendants
d d t are
‹ Best item size depends on the types of also locked.
transactions. ‹ DBMS should check hierarchical path before
granting lock.
‹ Intention lock could be used to lock all
ancestors of a locked node.
69 70

Levels of Locking Database Recovery


Process of restoring database to a correct state in
the event of a failure.
‹ Need for Recovery Control
– Two types of storage: volatile (main memory) and
nonvolatile.
– Volatile storage does not survive system crashes.
– Stable storage represents information that has
been replicated in several nonvolatile storage
media with independent failure modes.
71 72
Types of Failures Transactions and Recovery
‹ System crashes, resulting in loss of main ‹ Transactions represent basic unit of recovery.
memory. ‹ Recovery manager responsible for atomicity and
‹ Media failures, resulting in loss of parts of durability.
secondary storage.
‹ If failure occurs between commit and database
‹ Application
A li ti softwareft errors.
buffers being flushed to secondary storage then,
‹ Natural physical disasters.
to ensure durability, recovery manager has to
‹ Carelessness or unintentional destruction of redo (rollforward) transaction’s updates.
data or facilities.
‹ Sabotage.

73 74

Transactions and Recovery Example


‹ If transaction had not committed at failure time,
recovery manager has to undo (rollback) any
effects of that transaction for atomicity.
‹ Partial undo - only one transaction has to be
undone.
‹ Global undo - all transactions have to be ‹ DBMS starts at time t0, but fails at time tf. Assume data
for transactions T2 and T3 have been written to secondary
undone. storage.
‹ T1 and T6 have to be undone. In absence of any other
information, recovery manager has to redo T2, T3, T4, and
T5.
75 76

Recovery Facilities Log File


‹ DBMS should provide following facilities to ‹ Contains information about all updates to
assist with recovery: database:
– Backup mechanism, which makes periodic – Transaction records.
backup copies of database.
– Checkpoint
p records.
– Logging
L i f iliti
facilities, which
hi h keep
k t k off currentt
track
state of transactions and database changes. ‹ Often used for other purposes (for example,
– Checkpoint facility, which enables updates to auditing).
database in progress to be made permanent.
– Recovery manager, which allows DBMS to
restore database to consistent state following a
failure.
77 78
Log File Sample Log File
‹ Transaction records contain:
– Transaction identifier.
– Type of log record, (transaction start, insert,
update,
p , delete,, abort,, commit).
)
– Identifier of data item affected by database
action (insert, delete, and update operations).
– Before-image of data item.
– After-image of data item.
– Log management information.
79 80

Log File Checkpointing


‹ Log file may be duplexed or triplexed. Checkpoint
‹ Log file sometimes split into two separate Point of synchronization between database
random-access files. and log file. All buffers are force-written to
secondary storage.
‹ Potential bottleneck;; critical in determining
g
overall performance.
‹ Checkpoint record is created containing
identifiers of all active transactions.
‹ When failure occurs, redo all transactions that
committed after the checkpoint but before
failure and undo all transactions active at time of
crash.
81 82

Checkpointing Recovery Techniques


‹ In previous example, with checkpoint at time tc, ‹ If database has been damaged:
changes made by T2 and T3 have been written to – Need to restore last backup copy of database and
secondary storage. reapply updates of committed transactions using
log file.
‹ Thus:
‹ If database is only inconsistent:
– only redo T4 and T5, – Need to undo changes that caused inconsistency.
– undo transactions T1 and T6. May also need to redo some transactions to ensure
updates reach secondary storage.
– Do not need backup, but can restore database
using before- and after-images in the log file.

83 84
Main Recovery Techniques Deferred Update
‹ Three main recovery techniques: ‹ Updates are not written to the database until
after a transaction has reached its commit point.
– Deferred Update
‹ If transaction fails before commit, it will not have
– Immediate Update modified database and so no undoing of changes
– Shadow
Sh d Paging
P i required.
‹ May be necessary to redo updates of committed
transactions (using log file) as their effect may
not have reached database.

85 86

Immediate Update Immediate Update


‹ Updates are applied to database as they occur. ‹ If no “transaction commit” record in log, then
‹ May need to redo updates of committed that transaction was active at failure and must
transactions following a failure. be undone.
‹ Mayy need to undo effects of transactions that ‹ Undo operations are performed in reverse order
had not committed at time of failure. in which they were written to log.
‹ Essential that log records are written before
write to database. Write-ahead log protocol.

87 88

Shadow Paging Advanced Transaction Models


‹ Maintain two page tables during life of a ‹ Protocols considered so far are suitable for types
transaction: current page and shadow page table. of transactions that arise in traditional business
‹ When transaction starts, two pages are the same. applications, characterized by:
‹ Shadow page table is never changed thereafter – Data has many types, each with small number
andd is
i used
d to
t restore
t d t b
database i eventt off failure.
in f il of instances.
instances
‹ During transaction, current page table records – Designs may be very large.
all updates to database. – Design is not static but evolves through time.
‹ When transaction completes, current page table – Updates are far-reaching.
becomes shadow page table. – Cooperative engineering.

89 90
Advanced Transaction Models Advanced Transaction Models
‹ May result in transactions of long duration, ‹ We will look at two advanced transaction models:
giving rise to following problems:
– More susceptible to failure - need to minimize – Nested Transaction Model
amount of work lost. – Sagas
– May
M access large
l number
b off data
d t items
it -
concurrency limited if data inaccessible for
long periods.
– Deadlock more likely.
– Cooperation through use of shared data items
restricted by traditional concurrency
protocols.
91 92

Nested Transaction Model Nested Transaction Model


‹ Transaction viewed as hierarchy of subtransactions. ‹ Parent allowed to perform its own recovery:
‹ Top-level transaction can have number of child – Retry subtransaction.
transactions. – Ignore failure, in which case subtransaction is
‹ Each child can also have nested transactions. non-vital.
‹ In Moss’s proposal, only leaf-level subtransactions – Run contingency subtransaction.
allowed to perform database operations. – Abort.
‹ Transactions have to commit from bottom upwards. ‹ Updates of committed subtransactions at
‹ However, transaction abort at one level does not have intermediate levels are visible only within scope
to affect transaction in progress at higher level. of their immediate parents.

93 94

Nested Transaction Model Example of Nested Transactions


‹ Further, commit of subtransaction is
conditionally subject to commit or abort of its
superiors.
‹ Using this model, top-level transactions
conform to traditional ACID properties of flat
transaction.

95 96
Nested Transaction Model - Advantages Emulating Nested Transactions using Savepoints
‹ Modularity - transaction can be decomposed into An identifiable point in flat transaction
number of subtransactions for purposes of concurrency representing some partially consistent state.
and recovery.
‹ Finer level of granularity for concurrency control and ‹ Can be used as restart point for transaction if
recovery.
subsequent problem detected.
– Occurs at the level of subtransaction rather than the
transaction ‹ During execution of transaction, user can
‹ Intra-transaction parallelism. establish savepoint, which user can use to roll
– Subtractions can execute concurrently transaction back to.
‹ Intra-transaction recovery control. ‹ Unlike nested transactions, savepoints do not
– Uncommitted subtransactions can be aborted and rolled support any form of intra-transaction
back without any side effects to other subtransactions parallelism.
97 98

Sagas Sagas
“A sequence of (flat) transactions that can be ‹ Relax property of isolation by allowing saga to
interleaved with other transactions”. reveal its partial results to other concurrently
executing transactions before it completes.
‹ DBMS guarantees that either all transactions in ‹ Useful when subtransactions are relatively
saga are successfully
f ll completed
l t d or compensating
ti independent and compensating transactions can
transactions are run to undo partial execution. be produced.
‹ Saga has only one level of nesting. ‹ May be difficult sometimes to define
‹ For every subtransaction defined, there is
compensating transaction in advance, and DBMS
corresponding compensating transaction that may need to interact with user to determine
will semantically undo subtransaction’s effect. compensation.

99 100

You might also like