C H A P T E R14
Transactions
This chapter provides an overview of transaction processing.Itfirst motivates
the problems of atomicity,consistency,isolation and durability,and introduces
the notion of ACID transactions.It then presents some naive schemes,and their
drawbacks,thereby motivating the techniques described in Chapters15and16.
The rest of the chapter describes the notion of schedules and the concept of
serializability.
We strongly recommend covering this chapter in afirst course on databases, since it introduces concepts that every database student should be aware of.
Details on how to implement the transaction properties are covered in Chapters15
and16.
truncate的特征In the initial presentation to the ACID requirements,the isolation requirement on concurrent transactions does not insist on serializability.Following Haerder
and Reuter[1983],isolation just requires that the events within a transaction must
be hidden from other transactions running concurrently,in order to allow rollback.
However,later in the chapter,and in most of the book(except in Chapter26),
we use the stronger condition of serializability as a requirement on concurrent
transactions.
Exercises
14.12List the ACID properties.Explain the usefulness of each.
Answer:The ACID properties,and the need for each of them are:
•Consistency:Execution of a transaction in isolation(that is,with no other transaction executing concurrently)preserves the consistency
of the database.This is typically the responsibility of the application
programmer who codes the transactions.
•Atomicity:Either all operations of the transaction are reflected prop-erly in the database,or none are.Clearly lack of atomicity will lead to
inconsistency in the database.
115
116Chapter14Transactions
•Isolation:When multiple transactions execute concurrently,it should be the case that,for every pair of transactions T i and T j,it appears
to T i that either T jfinished execution before T i started,or T j started
execution after T ifinished.Thus,each transaction is unaware of other
transactions executing concurrently with it.The user view of a trans-
action system requires the isolation property,and the property that
concurrent schedules take the system from one consistent state to
another.These requirements are satisfied by ensuring that only seri-
alizable schedules of individually consistency preserving transactions
are allowed.
•Durability:After a transaction completes successfully,the changes it has made to the database persist,even if there are system failures.
14.13During its execution,a transaction passes through several states,until it
finally commits or aborts.List all possible sequences of states through
which a transaction may pass.Explain why each state transition may
occur.
Answer:The possible sequences of states are:-
a.active→partially committed→committed.This is the normal sequence
a successful transaction will follow.After executing all its statements
it enters the partially committed state.After enough recovery infor-
mation has been written to disk,the transactionfinally enters the
committed state.
b.active→partially committed→aborted.After executing the last state-
ment of the transaction,it enters the partially committed state.But
before enough recovery information is written to disk,a hardware
failure may occur destroying the memory contents.In this case the
changes which it made to the database are undone,and the transac-
tion enters the aborted state.
c.active→failed→aborte
d.After the transaction starts,if it is discov-
ered at some point that normal execution cannot continue(either
due to internal program errors or external errors),it enters the failed
state.It is then rolled back,after which it enters the aborted state.
14.14Explain the distinction between the terms serial schedule and serializable
schedule.
Answer:A schedule in which all the instructions belonging to one single
transaction appear together is called a serial schedule.A serializable schedule
has a weaker restriction that it should be equivalent to some serial schedule.
There are two definitions of schedule equivalence–conflict equivalence
and view equivalence.Both of these are described in the chapter.
14.15Consider the following two transactions:
Exercises117
T13:read(A);
read(B);
if A=0then B:=B+1;
write(B).
T14:read(B);
read(A);
if B=0then A:=A+1;
write(A).
Let the consistency requirement be A=0∨B=0,with A=B=0 the initial values.
a.Show that every serial execution involving these two transactions
preserves the consistency of the database.
b.Show a concurrent execution of T13and T14that produces a nonseri-
alizable schedule.
c.Is there a concurrent execution of T13and T14that produces a serial-
izable schedule?
Answer:
a.There are two possible executions:T13T14and T14T13.
Case1:A B
initially00
after T1301
after T1401
Consistency met:A=0∨B=0≡T∨F=T
Case2:A B
initially00
after T1410
after T1310
Consistency met:A=0∨B=0≡F∨T=T
b.Any interleaving of T13and T14results in a non-serializable schedule.
118Chapter14Transactions
T13T14
read(A)
read(B)
read(A)
read(B)
if A=0then B=B+1
if B=0then A=A+1
write(A)
write(B)
c.There is no parallel execution resulting in a serializable schedule.
From part a.we know that a serializable schedule results in A=0∨
B=0.Suppose we start with T13read(A).Then when the schedule
ends,no matter when we run the steps of T2,B=1.Now suppose
we start executing T14prior to completion of T13.Then T2read(B)
will give B a value of0.So when T2completes,A=1.Thus B=1∧A
=1→¬(A=0∨B=0).Similarly for starting with T14read(B).
14.16Give an example of a serializable schedule with two transactions such
that the order in which the transactions commit is different from the
serialization order.
Answer:
T1T2
read(A)
read(B)
unlock(A)
write(B)
read(A)
write(A)
commit
commit
As we can see,the schedule is serializable with an equivalent serial
schedule T1,T2.In the above schedule T2commits before T1.Note that the
unlock instruction is added to show how this schedule can occur even
Exercises119 with strict two-phase locking,where exclusive locks are held to commit, but shared locks can be released early in two-phase manner.
14.17What is a recoverable schedule?Why is recoverability of schedules desir-
able?Are there any circumstances under which it would be desirable to allow nonrecoverable schedules?Explain your answer.
Answer:A recoverable schedule is one where,for each pair of transac-tions T i and T j such that T j reads data items previously written by T i,the commit operation of T i appears before the commit operation of T j.Re-coverable schedules are desirable because failure of a transaction might otherwise bring the system into an irreversibly inconsistent state.Non-recoverable schedules may sometimes be needed when updates must be made visible early due to time constraints,even if they have not yet been committed,which may be required for very long duration transactions.
14.18Why do database systems support concurrent execution of transactions,
in spite of the extra programming effort needed to ensure that concurrent execution does not cause any problems?
Answer:Transaction-processing systems usually allow multiple trans-actions to run concurrently.It is far easier to insist that transactions run serially.However there are two good reasons for allowing concurrency:
•Improved throughput and resource utilization.A transaction my in-volve I/O activity,CPU activity.The CPU and the disk in a computer
system can operate in parallel.This can be exploited to run multiple
transactions in parallel.For example,while a read or write on behalf
of one transaction is in progress on one disk,another transaction can
be running in the CPU.This increases the throughput of the system.
•Reduced waiting time.If transactions run serially,a short transaction may have to wait for a preceding long transaction to complete.If
the transactions are operating on different parts of the database,it is
better to let them run concurrently,sharing the CPU cycles and disk
accesses among them.It reduces the unpredictable delays and the
average response time.
14.19Explain why the read-committed isolation level ensures that schedules
are cascade-free.
Answer:
The read-committed isolation level ensures that a transaction reads only the committed data.A transaction T i can not read a data item X which has been modified by a yet uncommitted concurrent transaction T j.This makes T i independent of the success or failure of T j.Hence,the schedules which follow read committed isolation level become cascade-free.
14.20For each of the following isolation levels,give an example of a schedule
that respects the specified level of isolation,but is not serializable:
a.Read uncommitted

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。