COP 5725 Fall 2012
Database Management
Systems
University of Florida, CISE Department Prof. Daisy Zhe Wang
Adapted Slides from Prof. Jeff Ullman
and from R. Ramakrishnan and J. Gehrke
Transaction Management Overview
Transactions
• A transaction is the DBMS’s abstract view of a user program: a sequence of reads and writes.
• A user’s program may carry out many operations on
the data retrieved from the database, but the DBMS is only concerned about what data is read/written
from/to the database.
•
Concurrent execution of user programs is essential for
good DBMS performance.– Because disk accesses are frequent, and relatively slow, it is important to keep the cpu humming by working on several user programs concurrently.
ACID Transactions
• A DBMS is expected to support “
ACID
transactions
,” processes that are:
–
Atomic
: Either the whole process is done ornone is.
–
Consistent
: Database constraints arepreserved.
–
Isolated
: It appears to the user as if only oneprocess executes at a time.
–
Durable
: Effects of a process do not get lost ifConcurrency in a DBMS
• Users submit transactions, and can think of each transaction as executing by itself.
– Concurrency is achieved by the DBMS, which interleaves
actions (reads/writes of DB objects) of various transactions.
– Each transaction must leave the database in a consistent state if the DB is consistent when the transaction begins.
• DBMS will enforce some ICs, depending on the ICs declared in CREATE TABLE statements.
• Beyond this, the DBMS does not really understand the semantics of the data. (e.g., it does not understand how the interest on a bank account is computed).
Atomicity of Transactions
•
A transaction might
commit
after completing all
its actions, or it could
abort
(or be aborted by
the DBMS) after executing some actions.
•
A very important property guaranteed by the
DBMS for all transactions is that they are
atomic
.
That is, a user can think of a Xact as
always executing all its actions in one step, or
not executing any actions at all.
Example
•
Consider two transactions (
Xacts
):
T1: BEGIN A=A+100, B=B-100 END T2: BEGIN A=1.06*A, B=1.06*B END
Intuitively, the first transaction is transferring $100 from B’s account to A’s account. The second is
crediting both accounts with a 6% interest payment.
There is no guarantee that T1 will execute before T2 or
vice-versa, if both are submitted together. However, the net effect must be equivalent to these two
Example (Contd.)
•
Consider a possible interleaving
(
schedule
)
:
T1: A=A+100, B=B-100
T2: A=1.06*A, B=1.06*B
This is OK. But what about:
T1: A=A+100, B=B-100 T2: A=1.06*A, B=1.06*B
The DBMS’s view of the second schedule:
Scheduling Transactions
•
Serial schedule: Schedule that does not interleave the
actions of different transactions.•
Equivalent schedules
: For any database state, the effect (on the set of objects in the database) ofexecuting the first schedule is identical to the effect of executing the second schedule.
•
Serializable schedule
: A schedule that is equivalent to some serial execution of the transactions.(Note: If each transaction preserves consistency, every serializable schedule preserves consistency. )
Anomalies with Interleaved Execution
•
Reading Uncommitted Data (WR
Conflicts, “dirty reads”):
•
Unrepeatable Reads (RW Conflicts):
T1: R(A), W(A), R(B), W(B), Abort T2: R(A), W(A), C
Anomalies (Continued)
•
Overwriting Uncommitted Data (WW
Conflicts):
Lock-Based Concurrency Control
•
Strict Two-phase Locking (Strict 2PL) Protocol:
– Each Xact must obtain a S (shared) lock on object before reading, and an X (exclusive) lock on object before writing.
– All locks held by a transaction are released when the transaction completes
• (Non-strict) 2PL Variant: Release locks anytime, but cannot acquire locks after releasing any lock.
– If an Xact holds an X lock on an object, no other Xact can get a lock (S or X) on that object.
• Strict 2PL allows only serializable schedules.
– Additionally, it simplifies transaction aborts
Aborting a Transaction
• If a transaction
Ti
is aborted, all its actions have to be undone. Not only that, ifTj
reads an object lastwritten by
Ti
,Tj
must be aborted as well!• Most systems avoid such cascading aborts by
releasing a transaction’s locks only at commit time.
– If Ti writes an object, Tj can read this only after Ti commits.
• In order to
undo
the actions of an abortedtransaction, the DBMS maintains a
log
in which every write is recorded.The Log
• The following actions are recorded in the log:
– Ti writes an object: the old value and the new value.
• Log record must go to disk before the changed page!
– Ti commits/aborts: a log record indicating this action.
• Log records are chained together by Xact id, so it’s easy to undo a specific Xact.
• Log is often
duplexed
andarchived
on stable storage.• All log related activities (and in fact, all CC related
Recovering From a Crash
• There are 3 phases in the
Aries
recovery algorithm:– Analysis: Scan the log forward (from the most recent checkpoint) to identify all Xacts that were active, and all dirty pages in the buffer pool at the time of the crash.
– Redo: Redoes all updates to dirty pages in the buffer pool, as needed, to ensure that all logged updates are in fact
carried out and written to disk.
– Undo: The writes of all Xacts that were active at the crash are undone (by restoring the before value of the update, which is in the log record for the update), working
Summary
• Concurrency control and recovery are among the most important functions provided by a DBMS.
• Users need not worry about concurrency.
– System automatically inserts lock/unlock requests and schedules actions of different Xacts in such a way as to ensure that the resulting execution is equivalent to
executing the Xacts one after the other in some order.
Transactions in SQL
•
SQL supports transactions, often behind
the scenes.
– Each statement issued at the generic query interface is a transaction by itself.
COMMIT
•
The SQL statement COMMIT causes a
transaction to complete.
– It’s database modifications are now
19
ROLLBACK
•
The SQL statement ROLLBACK also
causes the transaction to end, but by
aborting
.
– No effects on the database.
•
Failures like division by 0 or a
constraint violation can also cause
An Example: Interacting Processes
•
Assume the usual
Sells(bar,beer,price)
relation, and suppose that Joe’s Bar sells
only Bud for $2.50 and Miller for $3.00.
•
Sally is querying
Sells
for the highest and
lowest price Joe charges.
•
Joe decides to stop selling Bud and
21
Sally’s Program
•
Sally executes the following two SQL
statements, which we call
(min)
and
(max)
, to help remember what they do.
(max)
SELECT MAX(price) FROM Sells
WHERE bar = ’Joe’’s Bar’;
(min)
SELECT MIN(price) FROM Sells
Joe’s Program
•
At about the same time, Joe executes the
following steps, which have the mnemonic
names
(del)
and
(ins)
.
(del)
DELETE FROM Sells
WHERE bar = ’Joe’’s Bar’;
(ins)
INSERT INTO Sells
23
Interleaving of Statements
•
Although
(max)
must come before
(min)
, and
(del)
must come before
(ins)
, there are no other constraints on
the order of these statements, unless
we group Sally’s and/or Joe’s
Example: Strange Interleaving
•
Suppose the steps execute in the order
25
Fixing the Problem by Using Transactions
• If we group Sally’s statements
(max)(min)
into one transaction, then
she cannot see this inconsistency.
• She sees Joe’s prices at some fixed
time.
– Either before or after he changes prices, or in the middle, but the MAX and MIN are
Another Problem: Rollback
•
Suppose Joe executes
(del)(ins)
, not as
a transaction, but after executing these
statements, thinks better of it and
issues a ROLLBACK statement.
•
If Sally executes her statements after
(ins)
but before the rollback, she sees a
value, 3.50, that never existed in the
27
Solution
•
If Joe executes
(del)(ins)
as a
transaction, its effect cannot be seen by
others until the transaction executes
COMMIT.
Isolation Levels
•
SQL defines four
isolation levels
=
choices about what interactions are
allowed by transactions that execute at
about the same time.
•
How a DBMS implements these isolation
levels is highly complex, and a typical
29
Choosing the Isolation Level
•
Within a transaction, we can say:
SET TRANSACTION ISOLATION LEVEL
X
where
X
=
1. SERIALIZABLE
2. REPEATABLE READ 3. READ COMMITTED
Serializable Transactions
• If Sally = (max)(min) and Joe = (del)(ins)
are each transactions, and Sally runs with isolation level SERIALIZABLE, then she will see the database either before or after Joe runs, but not in the middle.
• It’s up to the DBMS vendor to figure out how
to do that, e.g.:
– True isolation in time.
31
Isolation Level Is Personal Choice
•
Your choice, e.g., run serializable,
affects only how
you
see the database,
not how others see it.
•
Example
: If Joe Runs serializable, but
Sally doesn’t, then Sally might see no
prices for Joe’s Bar.
– i.e., it looks to Sally as if she ran in the
Read-Commited Transactions
•
If Sally runs with isolation level READ
COMMITTED, then she can see only
committed data, but not necessarily the
same data each time.
•
Example: Under READ COMMITTED, the
interleaving
(max)(del)(ins)(min)
is
allowed, as long as Joe commits.
33
Repeatable-Read Transactions
•
Requirement is like read-committed,
plus: if data is read again, then
everything seen the first time will be
seen the second time.
Example: Repeatable Read
•
Suppose Sally runs under REPEATABLE
READ, and the order of execution is
(max)(del)(ins)(min)
.
– (max) sees prices 2.50 and 3.00.
– (min) can see 3.50, but must also see 2.50
35