Presentation is loading. Please wait.

Presentation is loading. Please wait.

3 Database Systems: Design, Implementation, and Management CHAPTER 9 Transaction Management and Concurrency Control.

Similar presentations


Presentation on theme: "3 Database Systems: Design, Implementation, and Management CHAPTER 9 Transaction Management and Concurrency Control."— Presentation transcript:

1 3 Database Systems: Design, Implementation, and Management CHAPTER 9 Transaction Management and Concurrency Control

2 4 Chapter Overview 4 What is a transaction 4 Concurrency control 4 Locks 4 Two-Phase Locking protocol

3 5 What Is a Transaction? l A transaction is a logical unit of work that must be either entirely completed or aborted; no intermediate states are acceptable. u Most real-world database transactions are formed by two or more database requests. u A database request is the equivalent of a single SQL statement in an application program or transaction. u A transaction that changes the contents of the database must alter the database from one consistent database state to another.

4 6 What Is a Transaction? Figure 9.1 Example of a Transaction

5 7 What Is a Transaction? l Evaluating Transaction Results u Examining the current balance for an account: SELECT ACC_NUM, ACC_BALANCE FROM CHECKACC WHERE ACC_NUM = ‘0908110638’; l The database remains in a consistent state after the transaction.

6 8 What Is a Transaction? l Evaluating Transaction Results u An accountant wishes to register the credit sale of 100 units of product X to customer Y in the amount of $500.00: l Reducing product X’s Quantity on hand by 100. l Adding $500.00 to customer Y’s accounts receivable. UPDATE PRODUCT SET PROD_QOH = PROD_QOH - 100 WHERE PROD_CODE = ‘X’; UPDATE ACCREC SET AR_BALANCE = AR_BALANCE + 500 WHERE AR_NUM = ‘Y’; l If the above two requests are not completely executed, the transaction yields an inconsistent database.

7 9 What Is a Transaction? l Transaction Properties u Atomicity requires that all operations of a transaction be completed; if not, the transaction is aborted. u Durability indicates the permanence of the database’s consistent state. u Serializability describes the result of the concurrent execution of several transactions. This property is important in multi-user and distributed databases. u Isolation means that the data used during the execution of a transaction cannot be used by a second transaction until the first one is completed. This is also relevant in a multi-user database. What about single-user systems with respect to each of these?

8 10 What Is a Transaction? l Transaction Management with SQL  Transaction support is provided by two SQL statements: COMMIT and ROLLBACK. u When a transaction sequence is initiated, it must continue through all succeeding SQL statements until one of the following four events occurs: l A COMMIT statement is reached. A ROLLBACK statement is reached. The end of a program is successfully reached ( COMMIT ). The program is abnormally terminated ( ROLLBACK ).

9 11 What Is a Transaction? l Transaction Management with SQL u Example: UPDATE PRODUCT SET PROD_QOH = PROD_QOH - 100 WHERE PROD_CODE = ‘345TYX’; UPDATE ACCREC SET AR_BALANCE = AR_BALANCE + 3500 WHERE AR_NUM = ‘60120010’; COMMIT;

10 12 What Is a Transaction? l The Transaction Log u A transaction log keeps track of all transactions that update the database. u The transaction log stores before-and-after data about the database and any of the tables, rows, and attribute values that participated in the transaction.  The information stored in the log is used by the DBMS for a recovery requirement triggered by a ROLLBACK statement or a system failure. u Roll back (for uncommitted) and Roll forward (for committed but not written to disk). u It is desirable to store the log file and the database files on separate disks to reduce the probability of simultaneous loss of both data.

11 13 What Is a Transaction? Table 9.1 Transaction Log

12 14 Concurrency Control l Concurrency control coordinates simultaneous execution of transactions in a multiprocessing database. u The objective of concurrency control is to ensure the serializability of transactions in a multi-user database environment. u Simultaneous execution of transactions over a shared database can create data integrity and consistency problems:

13 15 Concurrency Control l Example u Two concurrent transactions update PROD_QOH: TransactionComputation T1: Purchase 100 units  PROD_QOH = PROD_QOH + 100 T2: Sell 30 units  PROD_QOH = PROD_QOH - 30 u See Table 9.2 for the serial execution under normal circumstances. u See Table 9.3 for the lost update problems resulting from the execution of the second transaction before the first transaction is committed. Which property is violated?

14 16 Lost Updates, Uncommitted Data, Inconsistent Retrieval Table 9.2 Table 9.3 Lost Updates

15 17 Table 9.5 Table 9.4 Lost Updates, Uncommitted Data, Inconsistent Retrieval l T1 reads the data and modifies it and writes. l T2 reads the data not yet committed and modifies it l T1 rollsback l T2 writes Uncommitted Data

16 18 Table 9.6 Table 9.7 Lost Updates, Uncommitted Data, Inconsistent Retrieval l Inconsistent retrieval u Occurs when a transaction calculates some summary(aggregate) functions over a set of data while other transactions are updating it.

17 19 Inconsistent Retrievals (con’t.) Table 9.8

18 20 Concurrency Control l The Scheduler u The scheduler establishes the order in which the operations within concurrent transactions are executed. u The scheduler sequences the execution of database operations to ensure serializability. u To determine the appropriate order, the scheduler bases its actions on concurrency control algorithms. When two transactions access the same data and at least one involves a write operation, then there is potential for conflict.

19 21 LOCKS l Concurrency Control with Locking Methods u A lock guarantees exclusive use of a data item to a current transaction. u All lock information is managed by a lock manager. u Lock granularity indicates the level of lock use. l Database level l Table level l Page level l Row level l Field level

20 22 LOCKS l Database level Lock u Entire database is locked by a transaction u Good for batch processing but inefficient for on-line processing u When transaction T1 is accessing table A, transaction T2 has to wait for the lock to be released even if wants to access table B. l Table level Lock u Entire table is locked by a transaction u If a transaction requires access to several tables, each table may be locked u Less restrictive compared to database locks u Can cause unnecessary delays when two transactions need to access different parts of the same table u Not suitable for multi-user DBMS

21 23 Database-Level Locking Sequence Figure 9.2

22 24 Table-Level Lock Example Figure 9.3

23 25 LOCKS l Page Level Lock u A diskpage is locked by a transaction (4K or 8K or 16K) u A table may span many pages u A page contains many rows u This is most popular level of locking in multiuser databases l Row Level Lock u A row is locked by a transaction u Multiple transactions can access different rows in the same table concurrently u Less restrictive than page level locking u High overhead in lock management l Field Level Lock u Different transactions can lock different fields in the same row concurrently u Most flexible for mult-user data access u Requires very high overhead in managing locks

24 26 Page-Level Lock Example Figure 9.4

25 27 Row-Level Lock Example Figure 9.5

26 28 LOCKS l Binary Locks u A binary lock has only two states: locked (1) or unlocked (0). u If an object is locked by a transaction, no other transaction can use that object. u If an object is unlocked, any transaction can lock the object for its use. u A transaction must unlock the object after its termination. u Every transaction requires a lock and unlock operation for each data item that is accessed.

27 29 Example of Binary Lock Table Table 9.10

28 30 LOCKS l Exclusive Locks u An exclusive lock exists when access is specially reserved for the transaction that locked the object. u The exclusive lock must be used when the potential for conflict exists. u An exclusive lock is issued when a transaction wants to write a data item and no locks are currently held on that data item. l Shared Locks u A shared lock exists when concurrent transactions are granted READ access on the basis of a common lock. u A shared lock produces no conflict as long as the concurrent transactions are read only. u A shared lock is issued when a transaction wants to read data from the database and no exclusive lock is held on that data item.

29 31 Two-Phase Locking l The two-phase locking protocol defines how transactions acquire and relinquish locks. l In a growing phase, a transaction acquires all the required locks without unlocking any data. l Once all locks have been acquired, the transaction is in its locked point. All operations are executed at this time. l In a shrinking phase, a transaction releases all locks and cannot obtain any new locks. l This protocol guarantees serializability, but it does not prevent deadlocks. l No unlock operation can precede a lock operation in the same transaction. l No data are affected until all locks are obtained -- that is, until the transaction is in its locked point.

30 32 Two-Phase Locking Figure 9.6 Two-Phase Locking Protocol

31 33 Two-Phase Locking l Deadlocks (Deadly Embrace) u Deadlocks exist when two transactions T1 and T2 exist in the following mode: T1 = access data items X and Y T2 = access data items Y and X u If T1 has not unlocked data item Y, T2 cannot begin; and, if T2 has not unlocked data item X, T1 cannot continue. (See Table 9.11) u Several strategies have been developed to control deadlock situations.

32 34 Two-Phase Locking Table 9.11 How a Deadlock Condition Is Created

33 35 Two-Phase Locking l Deadlock Prevention u A transaction requesting for a new lock is aborted if there is a possibility of deadlock. Rollback and release acquired locks. l Deadlock Detection u DBMS periodically tests for existence of deadlock. If found kills one of the transactions. Rollback and release acquired locks. l Deadlock Avoidance u Must obtain all locks before it starts execution. It increases the response time. So, What is the best method?


Download ppt "3 Database Systems: Design, Implementation, and Management CHAPTER 9 Transaction Management and Concurrency Control."

Similar presentations


Ads by Google