my slide- transaction management

51
Rushdi Shams, Dept of CSE, KU ET 1 Database Systems Database Systems Transaction Management Transaction Management Version 1.0 Version 1.0

Upload: api-3754419

Post on 11-Apr-2015

375 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: My slide- Transaction management

Rushdi Shams, Dept of CSE, KUET 1

Database Database SystemsSystems

Transaction ManagementTransaction Management

Version 1.0Version 1.0

Page 2: My slide- Transaction management

2Rushdi Shams, Dept of CSE, KUET

TransactionTransaction

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

Transforms database from one Transforms database from one consistent state to another, consistent state to another, although consistency may be although consistency may be violated during transaction.violated during transaction.

Page 3: My slide- Transaction management

3Rushdi Shams, Dept of CSE, KUET

Outcomes of TransactionOutcomes of Transaction

Can have one of two outcomes:Can have one of two outcomes: SuccessSuccess - transaction - transaction commitscommits and database and database

reaches a new consistent state. reaches a new consistent state. FailureFailure - transaction - transaction abortsaborts, and database , and database

must be restored to consistent state before it must be restored to consistent state before it started. Such a transaction is started. Such a transaction is rolled backrolled back or or undoneundone. .

Committed transaction cannot be Committed transaction cannot be aborted.aborted.

Aborted transaction that is rolled Aborted transaction that is rolled back can be restarted later.back can be restarted later.

Page 4: My slide- Transaction management

4Rushdi Shams, Dept of CSE, KUET

State Transition Diagram of State Transition Diagram of TransactionTransaction

Page 5: My slide- Transaction management

5Rushdi Shams, Dept of CSE, KUET

Properties of Transaction Properties of Transaction ManagementManagement

There are 4 basic properties in There are 4 basic properties in transaction management-transaction management-

1.1. AtomicityAtomicity

2.2. ConsistencyConsistency

3.3. Isolation andIsolation and

4.4. DurabilityDurability

Their acronym is ACID!Their acronym is ACID!

Page 6: My slide- Transaction management

6Rushdi Shams, Dept of CSE, KUET

AACIDCID

AtomicityAtomicity

It is the ability of the DBMS that It is the ability of the DBMS that ensures either all of the transactions ensures either all of the transactions take place or none of them take take place or none of them take place!place!

Kind of weird, huh? Well, let me give Kind of weird, huh? Well, let me give an example. If a bank account is an example. If a bank account is debited, another bank account must debited, another bank account must be credited- that is called atomicity!be credited- that is called atomicity!

Page 7: My slide- Transaction management

7Rushdi Shams, Dept of CSE, KUET

AACCIDID ConsistencyConsistency

Every database has some rules Every database has some rules according to the organization to which it according to the organization to which it belongs. This property ensures that the belongs. This property ensures that the databases are in legal state after a databases are in legal state after a transaction takes place.transaction takes place.For example, if a bank says that its For example, if a bank says that its client’s account balance can never be client’s account balance can never be negative, then no such transaction will negative, then no such transaction will take place that makes a balance take place that makes a balance negative.negative.

Page 8: My slide- Transaction management

8Rushdi Shams, Dept of CSE, KUET

ACACIIDD

IsolationIsolationThis property ensures that no other This property ensures that no other operation can intervene the transaction operation can intervene the transaction operation.operation.For example, a bank manager, during a For example, a bank manager, during a transaction should be able to see transaction should be able to see balance of one account, not on both.balance of one account, not on both.

This is the most relaxed option in ACID This is the most relaxed option in ACID properties.properties.

Page 9: My slide- Transaction management

9Rushdi Shams, Dept of CSE, KUET

ACIACIDD

DurabilityDurability

This property ensures that once a This property ensures that once a transaction takes place, it cannot be transaction takes place, it cannot be undone.undone.

when an account to account transfer when an account to account transfer takes place, after completion, it should takes place, after completion, it should notify the user that transaction notify the user that transaction successfully done and you cannot rewind successfully done and you cannot rewind that!that!

Page 10: My slide- Transaction management

10Rushdi Shams, Dept of CSE, KUET

Concurrency ControlConcurrency Control Process of managing simultaneous Process of managing simultaneous

operations on the database without operations on the database without having them interfere with one another.having them interfere with one another.

Prevents interference when two or more Prevents interference when two or more users are accessing database users are accessing database simultaneously and at least one is simultaneously and at least one is updating data.updating data.

Although two transactions may be Although two transactions may be correct in themselves, interleaving of correct in themselves, interleaving of operations may produce an incorrect operations may produce an incorrect result.result.

Page 11: My slide- Transaction management

11Rushdi Shams, Dept of CSE, KUET

Why Concurrency Why Concurrency ControlControl

Three examples of potential Three examples of potential problems caused by problems caused by concurrency: concurrency:

1.1. Lost update problem.Lost update problem.

2.2. Uncommitted dependency Uncommitted dependency problem.problem.

3.3. Inconsistent analysis Inconsistent analysis problem. problem.

Page 12: My slide- Transaction management

12Rushdi Shams, Dept of CSE, KUET

Lost Update ProblemLost Update Problem

Successfully completed update is Successfully completed update is overridden by another user.overridden by another user.

TT11 withdrawing £10 from an withdrawing £10 from an account with balaccount with balxx, initially £100., initially £100.

TT22 depositing £100 into same depositing £100 into same account. account.

Serially, final balance would be Serially, final balance would be £190.£190.

Page 13: My slide- Transaction management

13Rushdi Shams, Dept of CSE, KUET

Lost Update Problem Lost Update Problem (continued)(continued)

Page 14: My slide- Transaction management

14Rushdi Shams, Dept of CSE, KUET

Uncommitted Dependency Uncommitted Dependency ProblemProblem

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

TT44 updates bal updates balxx to £200 but it aborts, to £200 but it aborts, so balso balxx should be back at original value should be back at original value of £100.of £100.

TT33 has read new value of bal has read new value of balxx (£200) (£200) and uses value as basis of £10 and uses value as basis of £10 reduction, giving a new balance of reduction, giving a new balance of £190, instead of £90.£190, instead of £90.

Page 15: My slide- Transaction management

15Rushdi Shams, Dept of CSE, KUET

Uncommitted Dependency Uncommitted Dependency Problem (continued)Problem (continued)

Page 16: My slide- Transaction management

16Rushdi Shams, Dept of CSE, KUET

Inconsistent Analysis Inconsistent Analysis ProblemProblem

Occurs when transaction reads several Occurs when transaction reads several values but second transaction updates values but second transaction updates some of them during execution of first. some of them during execution of first.

Sometimes referred to as Sometimes referred to as dirty readdirty read or or unrepeatable readunrepeatable read. .

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

Meantime, TMeantime, T55 has transferred £10 from bal has transferred £10 from balxx to balto balzz, so T, so T66 now has wrong result (£10 too now has wrong result (£10 too high).high).

Page 17: My slide- Transaction management

17Rushdi Shams, Dept of CSE, KUET

Inconsistent Analysis Inconsistent Analysis Problem (continued)Problem (continued)

Page 18: My slide- Transaction management

18Rushdi Shams, Dept of CSE, KUET

Serialzability Serialzability

Objective of a concurrency control Objective of a concurrency control protocol is to schedule protocol is to schedule transactions in such a way as to transactions in such a way as to avoid any interference. avoid any interference.

Could run transactions serially, but Could run transactions serially, but this limits degree of concurrency this limits degree of concurrency or parallelism in system.or parallelism in system.

Serializability identifies those Serializability identifies those executions of transactions executions of transactions guaranteed to ensure consistency.guaranteed to ensure consistency.

Page 19: My slide- Transaction management

19Rushdi Shams, Dept of CSE, KUET

Schedule Schedule

Sequence of reads/writes by set Sequence of reads/writes by set of concurrent transactions.of concurrent transactions.

There are 2 types of scheduling-There are 2 types of scheduling-

1.1. Serial schedulingSerial scheduling

2.2. Non serial schedulingNon serial scheduling

Page 20: My slide- Transaction management

20Rushdi Shams, Dept of CSE, KUET

Serial SchedulingSerial Scheduling

Schedule where operations of each Schedule where operations of each transaction are executed transaction are executed consecutively without any consecutively without any interleaved operations from other interleaved operations from other transactions. transactions.

Page 21: My slide- Transaction management

21Rushdi Shams, Dept of CSE, KUET

Non Serial SchedulingNon Serial Scheduling

Schedule where operations from Schedule where operations from set of concurrent transactions set of concurrent transactions are interleaved.are interleaved.

Page 22: My slide- Transaction management

22Rushdi Shams, Dept of CSE, KUET

Objectives of Objectives of SerializabilitySerializability

Objective of serializability is to find Objective of serializability is to find nonserial schedules that allow nonserial schedules that allow transactions to execute concurrently transactions to execute concurrently without interfering with one another.without interfering with one another.

In other words, want to find nonserial In other words, want to find nonserial schedules that are equivalent to schedules that are equivalent to somesome serial schedule. Such a schedule is serial schedule. Such a schedule is called called serializableserializable. .

Page 23: My slide- Transaction management

23Rushdi Shams, Dept of CSE, KUET

Order of Read/ WriteOrder of Read/ Write In serializability, ordering of In serializability, ordering of

read/writes is important:read/writes is important:1.1. If two transactions only read a data If two transactions only read a data

item, they do not conflict and order is item, they do not conflict and order is not important.not important.

2.2. If two transactions either read or write If two transactions either read or write completely separate data items, they do completely separate data items, they do not conflict and order is not important.not conflict and order is not important.

3.3. If one transaction writes a data item If one transaction writes a data item and another reads or writes same data and another reads or writes same data item, order of execution is important.item, order of execution is important.

Page 24: My slide- Transaction management

24Rushdi Shams, Dept of CSE, KUET

Conflict in Serializability Conflict in Serializability

Page 25: My slide- Transaction management

25Rushdi Shams, Dept of CSE, KUET

Precedence GraphPrecedence Graph

Used to test for serializabilityUsed to test for serializability CreateCreate

node for each transaction;node for each transaction; a directed edge Ta directed edge Tii T Tjj, if T, if Tjj reads the reads the

value of an item written by Tvalue of an item written by Tii;; a directed edge Ta directed edge Tii T Tjj, if T, if Tjj writes a writes a

value into an item after it has been value into an item after it has been read by Tread by Tii. .

If precedence graph contains cycle If precedence graph contains cycle you are in trouble!!you are in trouble!!

Page 26: My slide- Transaction management

26Rushdi Shams, Dept of CSE, KUET

Precedence Graph Precedence Graph ExampleExample

TT99 is transferring £100 from one is transferring £100 from one account with balance balaccount with balance balxx to to another account with balance another account with balance balbalyy..

TT1010 is increasing balance of these is increasing balance of these two accounts by 10%. two accounts by 10%.

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

Page 27: My slide- Transaction management

27Rushdi Shams, Dept of CSE, KUET

Precedence Graph Example Precedence Graph Example (continued)(continued)

Page 28: My slide- Transaction management

28Rushdi Shams, Dept of CSE, KUET

RecoverabilityRecoverability

Serializability identifies Serializability identifies schedules that maintain database schedules that maintain database consistency, assuming no consistency, assuming no transaction fails. transaction fails.

Could also examine Could also examine recoverability of transactions recoverability of transactions within schedule. within schedule.

Page 29: My slide- Transaction management

29Rushdi Shams, Dept of CSE, KUET

Recoverable ScheduleRecoverable Schedule

A schedule is recoverable if, for A schedule is recoverable if, for each pair of transactions Teach pair of transactions Tii and and TTjj, if T, if Tjj reads a data item reads a data item previously written by Tpreviously written by Tii, then the , then the commit operation of Tcommit operation of Tii precedes precedes the commit operation of Tthe commit operation of Tj.j.

Page 30: My slide- Transaction management

30Rushdi Shams, Dept of CSE, KUET

Concurrency Control Concurrency Control TechniquesTechniques

1.1. Optimistic method:Optimistic method:

BBased on assumption that conflict ased on assumption that conflict does not happen very often. So we does not happen very often. So we better let the transaction run to its better let the transaction run to its completion and then check at completion and then check at commit time to see if there is a commit time to see if there is a conflict. If there is, the offending conflict. If there is, the offending transaction simply starts again transaction simply starts again from the beginning. from the beginning.

Page 31: My slide- Transaction management

31Rushdi Shams, Dept of CSE, KUET

Concurrency Control Concurrency Control Techniques (continued)Techniques (continued)

2.2. Timestamping: Timestamping:

For any given database request, the For any given database request, the system compares the timestamping system compares the timestamping of the requesting transaction with the of the requesting transaction with the timestamp of the transaction that last timestamp of the transaction that last retrieved or updated the requested retrieved or updated the requested record. If there is a conflict, the record. If there is a conflict, the requesting transaction can simply be requesting transaction can simply be restarted (with new timestamping).restarted (with new timestamping).

Page 32: My slide- Transaction management

32Rushdi Shams, Dept of CSE, KUET

Concurrency Control Concurrency Control Techniques (continued)Techniques (continued)

3.3. Locking:Locking:When a transaction needs an When a transaction needs an assurance that transaction will not assurance that transaction will not be affected by others, it requires a be affected by others, it requires a lock on that object. Then the first lock on that object. Then the first transaction is able to carry out its transaction is able to carry out its processing without fear of others’ processing without fear of others’ interference.interference.

Page 33: My slide- Transaction management

33Rushdi Shams, Dept of CSE, KUET

LocksLocks

Exclusive Lock (XLock):Exclusive Lock (XLock): If If anybody is having exclusive lock, anybody is having exclusive lock, the other users will go into wait list the other users will go into wait list until the exclusive lock holder until the exclusive lock holder releases it.releases it.

Shared Lock (SLock):Shared Lock (SLock): If anybody If anybody has shared lock, the other users has shared lock, the other users with request shared lock can also with request shared lock can also have access to it.have access to it.

Page 34: My slide- Transaction management

34Rushdi Shams, Dept of CSE, KUET

Locks (continued)Locks (continued)If A has XLock,If A has XLock,

then if B wants XLock, B has to waitthen if B wants XLock, B has to wait then if B wants SLock, B has to waitthen if B wants SLock, B has to wait then if B wants NoLock, B can see itthen if B wants NoLock, B can see it If A has SLock,If A has SLock, then if B wants XLock, B has to waitthen if B wants XLock, B has to wait then if B wants SLock, B gets permissionthen if B wants SLock, B gets permission then if B wants NoLock, B can see itthen if B wants NoLock, B can see it If A has NoLock,If A has NoLock, then if B wants XLock, B will be grantedthen if B wants XLock, B will be granted then if B wants SLock, B will be grantedthen if B wants SLock, B will be granted then if B wants NoLock, B will be grantedthen if B wants NoLock, B will be granted

Xlock= Exclusive LockXlock= Exclusive Lock SLock= Shared LockSLock= Shared Lock

Page 35: My slide- Transaction management

35Rushdi Shams, Dept of CSE, KUET

Locks: Basic RulesLocks: Basic Rules

If transaction has shared lock on If transaction has shared lock on item, can read but not update item.item, can read but not update item.

If transaction has exclusive lock on If transaction has exclusive lock on item, can both read and update item, can both read and update item.item.

Reads cannot conflict, so more than Reads cannot conflict, so more than one transaction can hold shared one transaction can hold shared locks simultaneously on same item. locks simultaneously on same item.

Page 36: My slide- Transaction management

36Rushdi Shams, Dept of CSE, KUET

Need of a ProtocolNeed of a Protocol

Problem is that transactions Problem is that transactions release locks too soon, resulting release locks too soon, resulting in loss of total isolation and in loss of total isolation and atomicity. atomicity.

To guarantee serializability, need To guarantee serializability, need an additional protocol an additional protocol concerning the positioning of concerning the positioning of lock and unlock operations in lock and unlock operations in every transaction.every transaction.

Page 37: My slide- Transaction management

37Rushdi Shams, Dept of CSE, KUET

2 Phase Locking (2PL)2 Phase Locking (2PL)

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

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

but cannot release any locks.but cannot release any locks. Shrinking phaseShrinking phase - releases locks but - releases locks but

cannot acquire any new locks. cannot acquire any new locks.

Page 38: My slide- Transaction management

38Rushdi Shams, Dept of CSE, KUET

Preventing Lost Update Preventing Lost Update Problem using 2PLProblem using 2PL

Page 39: My slide- Transaction management

39Rushdi Shams, Dept of CSE, KUET

Preventing Uncommitted Preventing Uncommitted Dependency Problem using Dependency Problem using

2PL2PL

Page 40: My slide- Transaction management

40Rushdi Shams, Dept of CSE, KUET

Preventing Inconsistent Preventing Inconsistent Analysis Problem using 2PLAnalysis Problem using 2PL

Page 41: My slide- Transaction management

41Rushdi Shams, Dept of CSE, KUET

Cascading RollbackCascading Rollback

Page 42: My slide- Transaction management

42Rushdi Shams, Dept of CSE, KUET

Cascading Rollback Cascading Rollback (continued)(continued)

Transactions conform to 2PL. Transactions conform to 2PL. TT1414 aborts. aborts. Since TSince T1515 is dependent on T is dependent on T1414, T, T1515 must must

also be rolled back. Since Talso be rolled back. Since T1616 is is dependent on Tdependent on T1515, it too must be rolled , it too must be rolled back. back.

This is called This is called cascading rollbackcascading rollback.. To prevent this with 2PL, leave release To prevent this with 2PL, leave release

of of allall locks until end of transaction. locks until end of transaction.

Page 43: My slide- Transaction management

43Rushdi Shams, Dept of CSE, KUET

Dead LockDead Lock

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

Deadlock is a situation in which two Deadlock is a situation in which two or more transactions are in a or more transactions are in a simultaneous wait state; each one simultaneous wait state; each one waiting for one of the other to release waiting for one of the other to release a lock before it can proceed.a lock before it can proceed.

Page 44: My slide- Transaction management

44Rushdi Shams, Dept of CSE, KUET

Dead Lock (continued)Dead Lock (continued)

Page 45: My slide- Transaction management

45Rushdi Shams, Dept of CSE, KUET

Dead Lock (continued)Dead Lock (continued)

Deadlock should be transparent Deadlock should be transparent to user, so DBMS should restart to user, so DBMS should restart transaction(s).transaction(s).

Three general techniques for Three general techniques for handling deadlock: handling deadlock:

1.1. Timeouts.Timeouts.

2.2. Deadlock prevention.Deadlock prevention.

3.3. Deadlock detection and recovery. Deadlock detection and recovery.

Page 46: My slide- Transaction management

46Rushdi Shams, Dept of CSE, KUET

TimeoutsTimeouts

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

If lock has not been granted within this If lock has not been granted within this period, lock request times out. period, lock request times out.

In this case, DBMS assumes transaction In this case, DBMS assumes transaction may be deadlocked, even though it may may be deadlocked, even though it may not be, and it aborts and automatically not be, and it aborts and automatically restarts the transaction. restarts the transaction.

Page 47: My slide- Transaction management

47Rushdi Shams, Dept of CSE, KUET

Deadlock PreventionDeadlock Prevention

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

Wait-DieWait-Die - only an older transaction - only an older transaction can wait for younger one, otherwise can wait for younger one, otherwise If younger transaction requests lock held If younger transaction requests lock held

by olderby older

one, younger one is aborted (one, younger one is aborted (diesdies) and ) and restartedrestarted

with same timestamp.with same timestamp.

Page 48: My slide- Transaction management

48Rushdi Shams, Dept of CSE, KUET

Dead Lock Prevention Dead Lock Prevention (continued)(continued)

Wound-WaitWound-Wait - only a younger - only a younger transaction can wait for an older transaction can wait for an older one. one.

If older transaction requests lock If older transaction requests lock held by younger one, younger one held by younger one, younger one is aborted (is aborted (woundedwounded).).

Page 49: My slide- Transaction management

49Rushdi Shams, Dept of CSE, KUET

Dead Lock Detection & Dead Lock Detection & RecoveryRecovery

DBMS allows deadlock to occur but DBMS allows deadlock to occur but recognizes it and breaks it. recognizes it and breaks it.

Usually handled by construction of wait-Usually handled by construction of wait-for graph (WFG) showing transaction for graph (WFG) showing transaction dependencies:dependencies: Create a node for each transaction.Create a node for each transaction. Create edge TCreate edge Tii -> T -> Tjj, if T, if Tii waiting to waiting to

lock item locked by Tlock item locked by Tjj.. Deadlock exists if and only if WFG Deadlock exists if and only if WFG

contains cycle. contains cycle.

Page 50: My slide- Transaction management

50Rushdi Shams, Dept of CSE, KUET

Wait For Graph (WFG)Wait For Graph (WFG)

Page 51: My slide- Transaction management

51Rushdi Shams, Dept of CSE, KUET

ReferencesReferences

Database Management Systems by Database Management Systems by R. RamakrishnanR. Ramakrishnan

John Hall, Senior Lecturer, John Hall, Senior Lecturer, University of Bolton, UKUniversity of Bolton, UK