Presentation is loading. Please wait.

Presentation is loading. Please wait.

Transaction Management Transparencies. ©Pearson Education 2009 Chapter 14 - Objectives Function and importance of transactions. Properties of transactions.

Similar presentations


Presentation on theme: "Transaction Management Transparencies. ©Pearson Education 2009 Chapter 14 - Objectives Function and importance of transactions. Properties of transactions."— Presentation transcript:

1 Transaction Management Transparencies

2 ©Pearson Education 2009 Chapter 14 - Objectives Function and importance of transactions. Properties of transactions. Concurrency Control Meaning of serializability. How locking can ensure serializability. Deadlock and how it can be resolved. How timestamping can ensure serializability. Optimistic concurrency control. Granularity of locking. 2

3 ©Pearson Education 2009 Chapter 14 - Objectives Recovery Control Some causes of database failure. Purpose of transaction log file. Purpose of checkpointing. How to recover following database failure. 3

4 ©Pearson Education 2009 Transaction support Transaction Action, or series of actions, carried out by user or application, which reads or updates contents of database. Logical unit of work on the database. Application program is series of transactions with non-database processing in between. Transforms database from one consistent state to another, although consistency may be violated during transaction. 4

5 ©Pearson Education 2009 Example transaction 5

6 ©Pearson Education 2009 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. 6

7 ©Pearson Education 2009 Properties of a transaction Four basic (ACID) properties of a transaction are: Atomicity ‘All or nothing’ property. ConsistencyMust 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. 7

8 ©Pearson Education 2009 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. 8

9 ©Pearson Education 2009 Need for concurrency control Three examples of potential problems caused by concurrency: Lost update problem. Uncommitted dependency problem. Inconsistent analysis problem. 9

10 ©Pearson Education 2009 Lost update problem Successfully completed update is overridden by another user. T 1 withdrawing $10 from an account with bal x, initially $100. T 2 depositing $100 into same account. Serially, final balance would be $190. 10

11 ©Pearson Education 2009 Lost update problem Loss of T 2 ’s update avoided by preventing T 1 from reading bal x until after update. 11

12 ©Pearson Education 2009 Uncommitted dependency problem Occurs when one transaction can see intermediate results of another transaction before it has committed. T 4 updates bal x to $200 but it aborts, so bal x should be back at original value of $100. T 3 has read new value of bal x ($200) and uses value as basis of $10 reduction, giving a new balance of $190, instead of $90. 12

13 ©Pearson Education 2009 Uncommitted dependency problem Problem avoided by preventing T 3 from reading bal x until after T 4 commits or aborts. 13

14 ©Pearson Education 2009 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. T 6 is totaling balances of account x ($100), account y ($50), and account z ($25). Meantime, T 5 has transferred $10 from bal x to bal z, so T 6 now has wrong result ($10 too high). 14

15 ©Pearson Education 2009 Inconsistent analysis problem Problem avoided by preventing T 6 from reading bal x and bal z until after T 5 completed updates. 15

16 ©Pearson Education 2009 16 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.

17 ©Pearson Education 2009 17 Serializability Schedule Sequence of reads/writes by set of concurrent transactions. Serial Schedule Schedule 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.

18 ©Pearson Education 2009 18 Serializability 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.

19 ©Pearson Education 2009 19 Concurrency control protocols Two basic concurrency control techniques: Locking, Timestamping. Both are conservative approaches: delay transactions in case they conflict with other transactions. Optimistic methods assume conflict is rare and only check for conflicts at commit.

20 ©Pearson Education 2009 20 Locking methods Transaction uses locks to deny access to other transactions and so prevent incorrect updates. Most widely used approach to ensure serializability. Generally, a transaction must claim a shared (read) or exclusive (write) lock on a data item before read or write. Lock prevents another transaction from modifying item or even reading it, in the case of a write lock.

21 ©Pearson Education 2009 21 Locking – basic rules If transaction has shared lock on item, can read but not update item. If transaction has exclusive lock on item, can both read and update item. Reads cannot conflict, so more than one transaction can hold shared locks simultaneously on same item. Exclusive lock gives transaction exclusive access to that item.

22 ©Pearson Education 2009 22 Incorrect locking schedule

23 ©Pearson Education 2009 23 Incorrect locking schedule If at start, bal x = 100, bal y = 400, result should be: bal x = 220, bal y = 330, if T 9 executes before T 10, or bal x = 210, bal y = 340, if T 10 executes before T 9. However, result gives bal x = 220 and bal y = 340. S is not a serializable schedule. Problem is that transactions release locks too soon, resulting in loss of total isolation and atomicity. To guarantee serializability, need an additional protocol concerning the positioning of lock and unlock operations in every transaction.

24 ©Pearson Education 2009 24 Two- phase locking Transaction follows 2PL protocol if all locking operations precede first unlock operation in the transaction. Two phases for transaction: Growing phase - acquires all locks but cannot release any locks. Shrinking phase - releases locks but cannot acquire any new locks.

25 ©Pearson Education 2009 25 Preventing lost update problem

26 ©Pearson Education 2009 26 Preventing uncommitted dependency problem

27 ©Pearson Education 2009 27 Preventing inconsistent analysis problem

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

29 ©Pearson Education 2009 29 Deadlock Only one way to break deadlock: abort one or more of the transactions. Deadlock should be transparent to user, so DBMS should restart transaction(s). Three general techniques for handling deadlock: Timeouts. Deadlock prevention. Deadlock detection and recovery.

30 ©Pearson Education 2009 30 Timestamp methods Transactions ordered globally so that older transactions, transactions with smaller timestamps, get priority in the event of conflict. Conflict is resolved by rolling back and restarting transaction. No locks so no deadlock.

31 ©Pearson Education 2009 31 Timestamp methods Timestamp A unique identifier created by DBMS that indicates relative starting time of a transaction. Can be generated by using system clock at time transaction started, or by incrementing a logical counter every time a new transaction starts. Read/write proceeds only if last update on that data item was carried out by an older transaction. Otherwise, transaction requesting read/write is restarted and given a new timestamp.

32 ©Pearson Education 2009 32 Optimistic methods Based on assumption that conflict is rare and more efficient to let transactions proceed without delays to ensure serializability. At commit, check is made to determine whether conflict has occurred. If there is a conflict, transaction must be rolled back and restarted. Potentially allows greater concurrency than traditional protocols.

33 ©Pearson Education 2009 33 Optimistic methods Three phases: Read: from start to just before commit; read from database into local variables and update local data. Validation: check to ensure serializability is not violated; if violated transaction aborted and restarted. Write (for update transactions): updates made to local variables then applied to database.

34 ©Pearson Education 2009 34 Recovery Process of restoring database to a correct state in the event of a failure. Need for Recovery Control Two types of storage: volatile (main memory) and nonvolatile. Volatile storage does not survive system crashes. Stable storage represents information that has been replicated in several nonvolatile storage media with independent failure modes.

35 ©Pearson Education 2009 35 Types of Failure System crashes, resulting in loss of main memory. Media failures, resulting in loss of parts of secondary storage. Application software errors. Natural physical disasters. Carelessness or unintentional destruction of data or facilities. Sabotage.

36 ©Pearson Education 2009 36 Transactions and recovery Transactions represent basic unit of recovery. Recovery manager responsible for atomicity and durability. If failure occurs between commit and database buffers being flushed to secondary storage then, to ensure durability, recovery manager has to redo (rollforward) transaction’s updates. If transaction had not committed at failure time, recovery manager has to undo (rollback) any effects of that transaction for atomicity. Partial undo - only one transaction has to be undone. Global undo - all transactions have to be undone.

37 ©Pearson Education 2009 37 Transactions and recovery DBMS starts at time t 0, but fails at time t f. Assume data for transactions T 2 and T 3 have been written to secondary storage. T 1 and T 6 have to be undone. In absence of any other information, recovery manager has to redo T 2, T 3, T 4, and T 5.

38 ©Pearson Education 2009 38 Recovery facilities DBMS should provide following facilities to assist with recovery: Backup mechanism, which makes periodic backup copies of database. Logging facilities, which keep track of current state of transactions and database changes. Checkpoint facility, which enables updates to database in progress to be made permanent. Recovery manager, which allows DBMS to restore database to consistent state following a failure.

39 ©Pearson Education 2009 39 Log file Contains information about all updates to database: Transaction records. Checkpoint records. Often used for other purposes (for example, auditing).

40 ©Pearson Education 2009 40 Log file Transaction records contain: Transaction identifier. Type of log record, (transaction start, insert, update, delete, abort, commit). Identifier of data item affected by database action (insert, delete, and update operations). Before-image of data item. After-image of data item. Log management information.

41 ©Pearson Education 2009 41 Log file

42 ©Pearson Education 2009 42 Log file Log file may be duplexed or triplexed. Log file sometimes split into two separate random- access files. Potential bottleneck; critical in determining overall performance.

43 ©Pearson Education 2009 43 Checkpointing Checkpoint Point of synchronization between database and log file. All buffers are force-written to secondary storage. Checkpoint record is created containing identifiers of all active transactions. When failure occurs, redo all transactions that committed since the checkpoint and undo all transactions active at time of crash.

44 ©Pearson Education 2009 44 Checkpointing In previous example, with checkpoint at time t c, changes made by T 2 and T 3 have been written to secondary storage. Thus: only redo T 4 and T 5, undo transactions T 1 and T6.

45 ©Pearson Education 2009 45 Recovery techniques If database has been damaged: Need to restore last backup copy of database and reapply updates of committed transactions using log file. If database is only inconsistent: Need to undo changes that caused inconsistency. May also need to redo some transactions to ensure updates reach secondary storage. Do not need backup, but can restore database using before- and after-images in the log file.

46 ©Pearson Education 2009 46 Immediate update Updates are applied to database as they occur. Need to redo updates of committed transactions following a failure. May need to undo effects of transactions that had not committed at time of failure. Essential that log records are written before write to database. Write-ahead log protocol.

47 ©Pearson Education 2009 47 Immediate update If no “transaction commit” record in log, then that transaction was active at failure and must be undone. Undo operations are performed in reverse order in which they were written to log.


Download ppt "Transaction Management Transparencies. ©Pearson Education 2009 Chapter 14 - Objectives Function and importance of transactions. Properties of transactions."

Similar presentations


Ads by Google