20
CHAPTER 5 DATABASE TRANSACTION MANAGEMENT DFC2033 DATABASE SYSTEM

Chapter 5 Database Transaction Management

Embed Size (px)

Citation preview

Page 1: Chapter 5 Database Transaction Management

CHAPTER 5 DATABASE TRANSACTION MANAGEMENT

DFC2033 DATABASE SYSTEM

Page 2: Chapter 5 Database Transaction Management

Course Learning Outcome

CLO2 : Develop a database using a concurrency control and data recovery technique to manage the database system.

Page 3: Chapter 5 Database Transaction Management

Learning Outcome

¨  Describe the properties of database transaction: a. Atomic

b. Consistent

c. Isolated

d. Durable

¨  Perform a transaction of a given database using SQL statements.

¨  Use START TRANSACTION and COMMIT statements.

¨  State the purpose of concurrency control.

¨  Explain the interference problems arise from simultaneous access to database: a. Lost update

b. Uncommitted dependency

c. Inconsistent retrieval

¨  Describe the tools used by DBMS to prevent the interference problems : locks, Two-Phase Locking (2PL) protocol.

¨  Describe the recovery tools: a. Transaction log

b. Checkpoint

c. Database back-up

Page 4: Chapter 5 Database Transaction Management

What is Transaction ?

¨  A transaction is a unit of work that should be processed reliably without interference from other users and without loss of data due to failures.

¨  Examples of transactions are withdrawing cash at an ATM, making an airline reservation and registering for a course.

Page 5: Chapter 5 Database Transaction Management
Page 6: Chapter 5 Database Transaction Management
Page 7: Chapter 5 Database Transaction Management
Page 8: Chapter 5 Database Transaction Management
Page 9: Chapter 5 Database Transaction Management
Page 10: Chapter 5 Database Transaction Management
Page 11: Chapter 5 Database Transaction Management

Concurrency Control

¨  The purpose of concurrency control is to prevent two different users (or two different connections by the same user) from trying to update the same data at the same time. Concurrency control can also prevent one user from seeing out-of-date data while another user is updating the same data.

Page 12: Chapter 5 Database Transaction Management

Concurrency Control

The following examples explain why concurrency control is needed. For both examples, suppose that your checking account contains $1,000. During the day you deposit $300 and spend $200 from that account. At the end of the day your account should have $1,100.

¨  Example 1: No concurrency control

At 11:00 AM, bank teller #1 looks up your account and sees that you have $1,000. The teller subtracts the $200 check, but is not able to save the updated account balance ($800) immediately. At 11:01 AM, another teller #2 looks up your account and still sees the $1,000 balance. Teller #2 then adds your $300 deposit and saves your new account balance as $1,300. At 11:09 AM, bank teller #1 returns to the terminal, finishes entering and saving the updated value that is calculated to be $800. That $800 value writes over the $1300. At the end of the day, your account has $800 when it should have had $1,100 ($1000 + 300 - 200).

¨  Example 2: Concurrency control

When teller #1 starts working on your account, a lock is placed on the account. When teller #2 tries to read or update your account while teller #1 is updating your account, teller #2 will not be given access and gets an error message. After teller #1 has finished the update, teller #2 can proceed. At the end of the day, your account has $1,100 ($1000 - 200 + 300).

Page 13: Chapter 5 Database Transaction Management

Concurrency Control

Interference problem arise from simultaneous access to database: ¨  Lost Update ¨  Uncommitted Dependency ¨  Inconsistent Retrieval

Page 14: Chapter 5 Database Transaction Management

Lost Update

¨  Successfully completed update is overridden by another user.

¨  Lost updates occur when two or more transactions select the same row and then update the row based on the value originally selected.

¨  Each transaction is unaware of other transactions. The last updates overwrites updates made by the other transactions which results in lost data.

Page 15: Chapter 5 Database Transaction Management

Uncommitted Dependency

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

¨  Uncommitted dependency occurs when a second transaction selects a row that is being updated by another transaction.

¨  The second transaction is reading data that has not been committed yet and may be changed by the transaction updating the row.

Page 16: Chapter 5 Database Transaction Management

Inconsistent Retrieval

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

Page 17: Chapter 5 Database Transaction Management

Database Concurrency Control

Two-Phase Locking Techniques -The algorithm (a) Locking (Growing) (b) Unlocking (Shrinking). ¨  Locking (Growing) Phase: A transaction applies locks (read

or write) on desired data items one at a time. ¨  Unlocking (Shrinking) Phase: A transaction unlocks its locked

data items one at a time. Requirement: For a transaction these two phases must be mutually exclusively, that is, during locking phase unlocking phase must not start and during unlocking phase locking phase must not begin.

Page 18: Chapter 5 Database Transaction Management

Slide  18-­‐  18  

Deadlock

¨  Deadlock prevention ¤ A transaction locks all data items it refers to before it

begins execution. ¤ This way of locking prevents deadlock since a

transaction never waits for a data item. ¤ The conservative two-phase locking uses this approach.

Page 19: Chapter 5 Database Transaction Management

Slide  18-­‐  19  

Deadlock

¨  Deadlock detection and resolution ¤  In this approach, deadlocks are allowed to happen. The

scheduler maintains a wait-for-graph for detecting cycle. If a cycle exists, then one transaction involved in the cycle is selected (victim) and rolled-back.

¤  A wait-for-graph is created using the lock table. As soon as a transaction is blocked, it is added to the graph. When a chain like: Ti waits for Tj waits for Tk waits for Ti or Tj occurs, then this creates a cycle. One of the transaction o

Page 20: Chapter 5 Database Transaction Management

Types of Recovery Tools

¨  Transaction Log ¤ A table that contains a history of database changes.

The recovery manager uses the log table to recover from failures.

¨  Checkpoint ¤ The act of writing a checkpoint to log and writing log

and database buffer to disk.

¨  Database Backup ¤ A copy of all or part of disk. Is used when the disk

containing the database or log is damaged.