48
Transaction Management Dale-Marie Wilson, Ph.D.

Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

  • View
    220

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Transaction Management

Dale-Marie Wilson, Ph.D.

Page 2: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Transaction Support

TransactionAction, or series of actions, carried out by user or application, which reads or updates contents of database.

Logical unit of work Application program - series of transactions with

non-database processing in between Transforms database from one consistent state

to another Consistency may be violated during transaction

Page 3: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Transaction Example

Page 4: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Transaction Support

Two possible outcomes: Success - transaction commits and database

reaches new consistent state Failure - transaction aborts, and database

restored to consistent state before transaction started

• Referred to as rolled back or undone transaction

Committed transaction cannot be aborted Aborted transaction that is rolled back

can be restarted later

Page 5: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

State Transition Diagram for Transaction

Page 6: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Properties of Transaction

Basic (ACID) properties:

Atomicity ‘All or nothing’ property – transaction is an indivisible unit performed in entirety or not at all

Consistency Must transform database from one consistent state to another

Isolation Partial effects of incomplete transactions should not be visible to other transactions

DurabilityEffects of a committed transaction are permanent and must not be lost because of later failure

Page 7: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

DBMS Transaction Subsystem

Page 8: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Concurrency Control

Process of managing simultaneous operations on the database without having them interfere with one another

Prevents interference when two or more users access database simultaneously and at least one updates data

Interleaving of operations may produce incorrect results

Page 9: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Need for Concurrency Control

Potential concurrency problems: Lost update problem Uncommitted dependency problem Inconsistent analysis problem

Page 10: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Lost Update Problem

Successfully completed update overridden by another user

Example:T1 withdrawing £10 from an account with

balx, initially £100

T2 depositing £100 into same accountSerially, final balance would be £190

Page 11: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Lost Update Problem

Loss of T2’s update avoided by preventing T1 from reading balx until after update

Page 12: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Uncommitted Dependency Problem

Occurs when one transaction can see intermediate results of another transaction before it has committed

Example:T4 updates balx to £200 but it aborts, so balx

should be back at original value of £100T3 has read new value of balx (£200) and

uses value as basis of £10 reduction, giving a new balance of £190, instead of £90

Page 13: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Uncommitted Dependency Problem

Problem avoided by preventing T3 from reading balx until after T4 commits or aborts

Page 14: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Inconsistent Analysis Problem

Occurs when transaction reads several values but 2nd transaction updates some of them during execution of first

Aka dirty read or unrepeatable read Example:

T6 is totaling balances of account x (£100), account y (£50), and account z (£25).

Meantime, T5 has transferred £10 from balx to balz, so T6 now has wrong result (£10 too high).

Page 15: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Inconsistent Analysis Problem

Problem avoided by preventing T6 from reading balx and balz until after T5 completed updates

Page 16: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Serializability

Objective of concurrency control protocolTo schedule transactions to avoid

interference Transactions could be executed serially

Limits degree of concurrency or parallelism in system

Serializability identifies those executions of transactions guaranteed to ensure consistency

Page 17: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Serializability

ScheduleSequence of reads/writes by set of concurrent transactions

Serial ScheduleSchedule where operations of each transaction are executed consecutively without any interleaved operations from other transactions

No guarantee that results of all serial executions of a given set of transactions will be identical

Page 18: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Nonserial schedule

Schedule where operations from set of concurrent transactions are interleaved

Objective of serializability To find nonserial schedules that allow transactions

to execute concurrently without interfering with one another

Objective - to find nonserial schedules that are equivalent to some serial schedule Called serializable

Page 19: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Serializability

In serializability, ordering of read/writes is important:

(a) If two transactions only read data item• No conflict and order not important

(b) If two transactions either read or write completely separate data items• No conflict and order not important

(c) If one transaction writes a data item and another reads or writes same data item Order of execution important

Page 20: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Conflict Serializability Example

Page 21: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Serializability

Conflict serializable schedule orders conflicting operations to achieve serial execution

Under constrained write rule (transaction updates data item based on its old value, which is first read)Precedence graph used to test for

serializability

Page 22: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Precedence Graph

Create: node for each transactiondirected edge Ti Tj, if Tj reads value of

item written by TI

directed edge Ti Tj, if Tj writes value into item after read by Ti

If precedence graph contains cycle Not conflict serializable

Page 23: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Non-conflict Serializable Schedule Example

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

Page 24: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Non-conflict Serializable Schedule Example

Page 25: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Recoverability

Serializability identifies schedules that maintain database consistency Assumes no failed transactions

If transaction fails Atomicity requires effects of transaction to be

undone Durability states that once transaction commits,

its changes cannot be undone (without running another, compensating, transaction)

Page 26: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Recoverable Schedule

A schedule where, for each pair of transactions Ti and Tj, if Tj reads a data item previously written by Ti, then the commit operation of Ti precedes the commit operation of Tj

Page 27: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Concurrency Control Techniques

Conservative approaches Delay transactions in case they conflict with

other transactions Optimistic methods

Assume conflict rare and only check for conflicts at commit

Two conservative concurrency control techniques: Locking Timestamping

Page 28: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Locking

Transaction uses locks to deny access to other transactions and so prevent incorrect updates

Most widely used approach to ensure serializability

Transaction claims shared (read) or exclusive (write) lock on data item before read or write

Lock prevents another transaction from modifying item or even reading it, in the case of a write lock

Page 29: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Basic Locking Rules If transaction has shared lock on item

Can read; not update item If transaction has exclusive lock on item

Can read and update item More than one transaction can hold shared locks

simultaneously on same item Reads cannot conflict

Exclusive lock Gives transaction exclusive access to that item

Some systems allow transaction to: Upgrade read lock to exclusive lock Downgrade exclusive lock to shared lock

Page 30: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Incorrect Locking Schedule

For two transactions above, a valid schedule using these rules is:

S = {write_lock(T9, balx), read(T9, balx), write(T9, balx), unlock(T9, balx), write_lock(T10, balx), read(T10, balx), write(T10, balx), unlock(T10, balx), write_lock(T10, baly), read(T10, baly), write(T10, baly), unlock(T10, baly), commit(T10), write_lock(T9, baly), read(T9, baly), write(T9, baly), unlock(T9, baly), commit(T9) }

Page 31: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Incorrect Locking Schedule

If at start, balx = 100, baly = 400, result should be:

balx = 220, baly = 330, if T9 executes before T10, or balx = 210, baly = 340, if T10 executes before T9.

However, result gives balx = 220 and baly = 340

S is not a serializable schedule Problem:

Transactions release locks too soon => results in loss of total isolation and atomicity

To guarantee serializability Additional protocol concerning positioning of lock and

unlock operations in every transaction needed

Page 32: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Two-Phase Locking (2PL)

Transaction follows 2PL protocol if all locking operations precede first unlock operation in transaction

Two phases for transaction:Growing phase - acquires all locks but

cannot release any locksShrinking phase - releases locks but

cannot acquire any new locks

Page 33: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Preventing Lost Update Problem using 2PL

Page 34: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Preventing Uncommitted Dependency Problem using 2PL

Page 35: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Preventing Inconsistent Analysis Problem using 2PL

Page 36: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Cascading Rollback

If every transaction in schedule follows 2PL, Schedule is serializable

Problems occur with interpretation of when locks can be released

Page 37: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Cascading Rollback

Page 38: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Cascading Rollback

Transactions conform to 2PL T14 aborts

Since T15 dependent on T14, T15 must also be rolled back

Since T16 dependent on T15, T16 must be rolled back

To prevent this with 2PL Leave release of all locks until end of transaction

Page 39: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Deadlock

An impasse that may result when two (or more) transactions are each waiting for locks held by the other to be released

Page 40: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Deadlock

To break deadlock: abort one or more of transactionsDeadlock transparent to userDBMS restarts transaction(s)

Three techniques for handling deadlock: TimeoutsDeadlock preventionDeadlock detection and recovery

Page 41: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Timeouts

Transaction that requests lock will only wait for system-defined period of time

If lock not been granted within periodLock request times out

DBMS assumes transaction deadlocked => aborts and automatically restarts the transaction

Page 42: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Deadlock Prevention

DBMS looks ahead to see if transaction would cause deadlock and never allows deadlock to occur

Can order transactions using transaction timestamps: Wait-Die - only older transaction can wait

for younger one, otherwise transaction aborted (dies) and restarted with same timestamp

Wound-Wait - only younger transaction can wait for older one. If older transaction requests lock held by younger one, younger one aborted (wounded)

Page 43: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Recovery from Deadlock Detection

Several issues:Choice of deadlock victimHow far to roll transaction backAvoiding starvation – same transaction

always deadlock victim

Page 44: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Timestamping

Transactions ordered globally: Older transactions, transactions with

smaller timestamps, get priority in event of conflict

Conflict resolved by rolling back and restarting transaction

No locks => no deadlock

Page 45: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Timestamping

Timestamp

A unique identifier created by DBMS that indicates relative starting time of a transaction

Generated:Using system clock at start of transactionIncrementing logical counter every time

new transaction starts

Page 46: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Timestamping

Read/write proceeds only if last update on that data item carried out by older transaction

Else, transaction requesting read/write restarted and given new timestamp

Timestamps for data items: read-timestamp - timestamp of last transaction to

read item write-timestamp - timestamp of last transaction to

write item

Page 47: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Comparison of methods

Page 48: Transaction Management Dale-Marie Wilson, Ph.D.. Transaction Support Transaction Action, or series of actions, carried out by user or application, which

Chapter 22