Chapter10_TransactionManagementandConcurrencyControl
Chapter10_TransactionManagementandConcurrencyControl
Transaction Management
and Concurrency Control
CSC 3326
Learning Objectives
• After completing this chapter, you will be able to:
• Describe the database transaction management process
• Identify the four properties of a database transaction
• Explain concurrency control and its role in maintaining database integrity
• Describe concurrency control mechanisms: locking
Introduction
• Database transactions reflect real-world transactions that constitute a sequence
of multiple operations performed on a database, and all served as a single logical
unit of work.
• Transactions are likely to contain many parts
• All parts of a transaction must be successfully completed to prevent data
integrity problems.
• Executing and managing transactions are important database system activities.
• SQL transaction is a single unit of work applied to a database
• It is a sequence of ordered operations performed on the database.
Sales transaction: Sell a product to a customer
•You must write a new customer invoice.
•You must write a new invoice line
•You must reduce the quantity on hand in the product’s inventory.
•You must update the customer balance.
•You must insert a new account transaction.
Transaction
• A transaction is a logical unit of work that must be entirely completed or entirely
aborted.
• It is any action that reads from or writes to a database.
• It consists of:
• A SELECT statement to read data from table
• Series of related UPDATE statements
• Series of INSERT statements
• Series of DELETE statements
• Combination of SELECT, UPDATE, INSERT and DELETE statements
• A Transaction may consist of a single SQL statement or a collection of related SQL
statements.
Transaction
• Suppose a problem occurs after updating the product table and customer 10016 was not
charged => DBMS will roll back the database to a previous consistent state.
• The DBMS cannot guarantee that the semantic meaning of the transaction truly
represents the real-world event.
• While the DBMS executes transactions that modify the database, it also automatically updates the
transaction log.
• It stores the following information:
A record for the beginning of the transaction.
For each transaction component (SQL statement):
– The type of operation being performed (INSERT, UPDATE, DELETE).
– The names of the objects affected by the transaction (the name of the table).
– The “before” and “after” values for the fields being updated.
– Pointers to the previous and next transaction log entries for the same transaction.
• Several methods have been proposed to schedule the execution of conflicting operations
in concurrent transactions: locking, time stamping, and optimistic
Locking Methods
• Locking methods are used most frequently.
• Locking methods facilitate isolation of data items used in concurrently
executing transactions
• Lock: guarantees exclusive use of a data item to a current transaction. Transaction
T2 does not have access to a data item that is currently being used by transaction
T1.
• Required to prevent another transaction from reading inconsistent data.
• Lock manager: DBMS component responsible for assigning and policing the locks
used by the transactions
Lock Granularity
• Lock granularity indicates the level of lock use. Locking can take place at
the following levels: database, table, page, row, or even field (attribute).
• DB level: In a database-level lock, the entire database is locked, thus
preventing the use of any tables in the database by transaction T2 while
transaction T1 is being executed.
Lock Granularity
• In a table-level lock, the entire table is locked, preventing access to any row
by transaction T2 while transaction T1 is using the table.
Lock Granularity
• In a page-level lock, the DBMS locks an entire diskpage.
• A page is a section of disk that has a fixed size, such as 4K, 8K, or 16K
• A table can span several pages, and a page can contain several rows of one or
more tables.
• Page-level locks are currently the most frequently used locking method for
multiuser DBMSs
Lock Granularity
• A row-level lock is much less restrictive than the locks
• The DBMS allows concurrent transactions to access different rows of the same
table even when the rows are located on the same page
• Its management requires high overhead because a lock exists for each row in
a table of the database involved in a conflicting transactions
Lock Types/Modes
• Binary lock
• Two states: locked (1) and unlocked (0)
• If an object is locked by a transaction, no other transaction can use that object
• If an object is unlocked, any transaction can lock the object for its use
• Binary locks are considered too restrictive to yield optimal concurrency conditions
• DBMS will not allow two transactions to read the same database object even
though neither transaction updates the database and therefore no concurrency
problems can occur.
Lock Types/Modes
• Exclusive/Shared
• An exclusive lock exists when access is reserved specifically for the transaction that locked the object
• The exclusive lock must be used when the potential for conflict exists
• Shared lock exists when concurrent transactions are granted read access based on a common lock.
• 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.
• Because the two read transactions can be safely executed at once, shared locks allow several read transactions to
read the same data item concurrently=> Two transactions can share the same lock if they are read-only
• The exclusive lock is granted if and only if no other locks are held on the data item
• Using the shared/exclusive locking concept, a lock can have three states: unlocked, shared (read), and exclusive
(write).
• Basic Locking may have some problems:
The resulting transaction schedule might not be serializable.
S-LOCK: Shared locks for reads.
X-LOCK: Exclusive locks for writes.