03-transaction-management

advertisement
Sekolah Tinggi Ilmu Statistik (STIS)
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
1
Lecture 2
Dr. Said Mirza Pahlevi, M.Eng.
2
Today’s Lecture

Transaction
 Function and importance of transactions.
 Properties of transactions.

Concurrency Control
 Meaning of serializability
 Conflict serializability
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
3
First Topic
Dr. Said Mirza Pahlevi, M.Eng.
4
Transaction Support

Transaction is:
 An action, or series of actions, carried out by user or application,
which reads or updates contents of database.
 A logical unit of work on the database; it may be an entire program,
apart of a program, or a single command (e.g., SQL INSERT)

Application program execution is:
 One or more transactions with non-database processing taking
place in between

Transaction transforms database from one consistent
state to another, although consistency may be violated
during transaction.
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
5
Example Transaction 1

Update the salary of a
particular member of staff
given the staff number

A transaction consisting of
two database operations:
read and write and a nonStaff (staffNo, fName, lName,
database operation (salary
position, sex, DOB, salary,
= salary x 1.1)
branchNo)
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
6
Example Transaction 2

Delete the member of staff
with a given staff number x

As well as having to delete
the tuple in the Staff relation,
we also need to PropertyFor
Rent tuple managed by the
staff to a different member
PropertyForRent
(propertyNo, street, city,
postcode, type, rooms, rent,
ownerNo, staffNo, branchNo)
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
7
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; Such a transaction is
rolled back or undone.

Committed transaction cannot be aborted.

Aborted transaction that is rolled back can be
restarted later.
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
8
State Transition Diagram for Transaction
READ/WRITE

PARTIALLY COMMITTED: occurs after the final statement
has been executed
 Abort if has violated serializability or violated an integrity constraint

FAILED: occurs if the transaction cannot be committed or
the transaction is aborted while in the ACTIVE state
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
9
Transaction Delimitter

DBMS has no way of knowing which updates are
grouped together to form a single logical
transaction

The way to indicate transaction
 Keywords BEGIN TRANSACTION, COMMIT, ROLLBACK

If there are no delimitters, the entire program is
usually regarded as a single transaction
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
10
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.
 DBMS can ensure consistency by enforcing the constraints that
have been specified on the database schema, such as integrity
and enterprise/general constraints.
 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.
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
11
DBMS Transaction
Subsystem
Transaction manager:
coordinate transaction on behalve
of application program
Scheduler: responsible for
implementing a particular
strategy for concurrency control
Recovery manager: if a
failure occur during the
transaction, it ensures that
database is restored to the
previous state
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
Buffer manager: responsible
for efficient transfer of data
between disk storage & main
12
memory
Second Topic
Dr. Said Mirza Pahlevi, M.Eng.
13
Topics
1.
2.
3.
Concurrency control and serializability
Conflict serializability
View serializability next week
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
14
First Subtopic
Dr. Said Mirza Pahlevi, M.Eng.
15
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.
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
16
Need for Concurrency Control

Three examples of potential problems caused by
concurrency:
 Lost update problem.
 Uncommitted dependency problem.
 Inconsistent analysis problem.
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
17
Lost Update Problem

Successfully completed update is overridden by
another user.
1. T1 withdrawing £10 from an account with balx, initially £100.
2. T2 depositing £100 into same account.
3. Serially, final balance would be £190.
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
18
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 balance of £190, instead of £90.
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
19
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, account y, and account z.
 Meantime, T5 has transferred £10 from balx to balz , so T6 now has
wrong result (£10 too high)
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
20
Transaction Schedule

Multiple transactions can be executed concurrently
by interleaving their operations

Ordering of execution of operations from various
transactions T1, T2, … , Tn is called a schedule S

Constraint: for each transaction Ti, the order in
which operations occur in S must be the same as in
Ti

Only interested in read(r), write(w), commit(c),
abort(a)
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
21
Serializability

Objective of a concurrency control protocol is to
schedule transactions in such a way as to avoid
any interference.

Could run transactions serially, but this limits
degree of concurrency or parallelism in system.

Serializability identifies those executions of
transactions guaranteed to ensure consistency.
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
22
Serializability

Schedule

Serial Schedule

No guarantee that results of all serial executions of
a given set of transactions will be identical.
 Sequence of reads/writes by set of concurrent transactions.
 Schedule where operations of each transaction are executed
consecutively without any interleaved operations from other
transactions.
 e.g., in banking, it matters whether interest is calculated on an
account before a large deposit is made or after
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
23
Nonserial Schedule

Schedule where operations from set of concurrent
transactions are interleaved.

Objective of serializability is to find nonserial
schedules that allow transactions to execute
concurrently without interfering with one
another.

In other words, want to find nonserial schedules
that are equivalent to some serial schedule. Such a
schedule is called serializable.
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
24
Second Subtopic
Dr. Said Mirza Pahlevi, M.Eng.
25
Serializability

In serializability, ordering of read and write is
important:
a.
If two transactions only read a data item, they do not conflict and
order is not important (Nonconflicting)
 e.g., read1(x);read2(x) = read2(x);read1(x)
b.
If two transactions either read or write completely separate data
items, they do not conflict and order is not important
(nonconflicting).
 e.g ., read1(x);write2(y) = write2(y);read1(x)
c.
If one transaction writes a data item and another reads or writes
same data item, order of execution is important. (conflicting)
 read1(x);write2(x)
 write1(x);read2(x)
 write1(x);write2(x)
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
26
Conflicting Operations

Two operations conflict if they satisfy all three
conditions:
 They belong to different transactions and
 They access the same item and
 At least one is a write operation

Example:
 S: r1(X); r2(X); w1(X); r1(Y); w2(X); w1(Y);
conflicts
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
27
Conflict Equivalent

S1, S2 are conflict equivalent schedules if S1 can be
transformed into S2 by a series of swaps on nonconflicting operations
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
28
Conflict Serializable

A schedule is conflict serializable if it is conflict
equivalent to some serial schedule.

It is the condition that the schedulers in
commercial systems generally use when they need
to guarantee serializability
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
29
Example of Conflict Serializability
The order of
conflicting
operations
are unchanged
conflicting
conflicting
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
30
Precedence Graph

Use precedence graph to test for serializability

Create:

If precedence graph contains cycle, schedule is
NOT conflict serializable.
 Node for each transaction;
 A directed edge Ti  Tj, if Tj reads the value of an item
j
written by Ti; i
 A directed edge Ti  Tj, if Tj writes a value into an item after
i
it has been read by Ti.
j
 A directed edge Ti  Tj, if Tj writes a value into an item after
j
it has been written by Ti. i
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
31
Example: Non-conflict serializable
schedule
T9  T10
T9 T10
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)

T9 is transferring
£100 from one
account with balance
balx to another
account with balance
baly.

T10 is increasing
balance of these two
accounts by 10%.

Precedence graph has
a cycle and so is not
serializable.
32
Exercise

Schedule S1: r1(A); w1(A); r2(A); w2(A); r1(B);
w1(B); r2(B); w2(B)
 S1 is a conflict-serializable with T1  T2

Schedule S2: r2(A); r1(B); w2(A); r3(A); w1(B);
w3(A); r2(B); w2(B)
 S2 is a conflict-serializable with T1  T2  T3
Dr. Said Mirza Pahlevi, M.Eng.
Sekolah Tinggi Ilmu Statistik (STIS)
33
Download