0% found this document useful (0 votes)
12 views

Chapter10_TransactionManagementandConcurrencyControl

Database class

Uploaded by

chamso Abou
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Chapter10_TransactionManagementandConcurrencyControl

Database class

Uploaded by

chamso Abou
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Chapter 10

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.

• Improper or incomplete transactions can have devastating effect on database


integrity
• Other integrity rules (referential and entity )are automatically enforced by the DBMS
Transaction Properties (ACID)
• Atomicity requires that all operations (SQL requests) of a transaction be completed; if not,
the transaction is aborted.
• Consistency indicates the permanence of the database’s consistent state. A consistent
database state is one in which all data integrity constraints are satisfied.
 Explicitly specified integrity constraints such as primary keys and foreign keys
 Implicit integrity constraints: for example: sum of balances of all accounts

• A transaction takes a database from one consistent state to another.


• 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. Each transaction must be unaware of
other concurrently executing transactions. Intermediate transaction results must be hidden
from other concurrently executed transactions.
• Durability ensures that once transaction changes are done and committed, they cannot be
undone or lost, even in the event of a system failure.
Atomicity: if the transaction fails after step 3 and
before step 6, money will be “lost” leading to an
inconsistent database state

Consistency: the sum of A and B is unchanged by


the execution of the transaction

Durability: Once the user has been notified that


the transaction has completed (the updates to the
database by the transaction must persist even if
there are software or hardware failures)

Isolation: if between steps 3 and 4, another


transaction T2 is allowed to access the partially
updated database, it will see an inconsistent
database (the sum A + B will be less than it should
be).
Transaction Management with SQL
• AINSI defined standards that govern SQL database transactions.
• SQL statements that provide transaction support:
• COMMIT
• ROLLBACK

• AINSI standards: transaction sequence is initiated until:


• COMMIT statement is reached
• ROLLBACK statement is reached

• Not all SQL implementations follow the ANSI standard;


• Some implementations use transaction management statements such as the
following to indicate the beginning of a new transaction:
BEGIN/Start TRANSACTION;
Transaction Log
• Transaction log keeps track of all transactions that update the database
• DBMS uses the information stored in a log for :
• Recovery requirement triggered by a ROLLBACK statement
• System failure

• 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.

• The ending (COMMIT) of the transaction


Concurrency Control
• Isolation can be ensured by running transactions serially
• However, multiple transactions are allowed to run concurrently in the system.
• Advantages of concurrent transaction execution are:
 Increased processor and disk utilization, leading to better transaction throughput
 Reduced average response time for transactions
• Coordinating the simultaneous execution of transactions in a multiuser database system is
known as concurrency control.
• Concurrency control aims at preserving the isolation property of concurrently executing
transactions.
• Crucial because the simultaneous execution of transactions over a shared database can
create several data integrity and consistency problems
• Three main problems are lost updates, uncommitted data, and inconsistent retrievals
Lost Updates
• The lost update problem occurs when two concurrent transactions, T1 and
T2, are updating the same data element and one of the updates is lost
(overwritten by the other transaction).
Uncommitted Data
• Uncommitted data
• Occurs when:
• Two transactions are executed concurrently
• First transaction is rolled back after the second transaction has already accessed
uncommitted data
Inconsistent retrievals

• Inconsistent retrievals occur when a transaction accesses data


before and after one or more other transactions finish working with
such data.
The scheduler
• The scheduler is a special DBMS process that establishes the order in
which the operations are executed within concurrent transactions.
• The scheduler interleaves the execution of database operations to ensure
serializability and isolation of transactions.
• The scheduler’s main job is to create a serializable schedule of a
transaction’s operations, in which the interleaved execution of the
transactions (T1, T2, T3, etc.) yields the same results as if the transactions
were executed in serial order (one after another).
• Not all transactions are serializable.
Serializability
The Scheduler
• The scheduler facilitates data isolation to ensure that two transactions do not update the
same data element at the same time.
• Database operations might require READ and/or WRITE actions that produce conflicts
• Possible conflict scenarios when two transactions, T1 and T2, are executed concurrently over
the same data.

• 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.

An example of a non- serializable


schedule using exclusive and
shared locks.
Two-Phase Locking to Ensure
Serializability
• Ensure schedule serializability through locks.
• Defines how transactions acquire and relinquish locks
• A growing phase, in which a transaction acquires all required locks without unlocking any data.
Once all locks have been acquired, the transaction is in its locked point.
• A shrinking phase, in which a transaction releases all locks and cannot obtain a new lock.
• As soon as a transaction releases one lock, it enters the shrinking phase. After this point, it cannot
get any new locks.
• Schedules generated by two phase locking (2PL) are always serializable because:
 No unlock operation can precede a lock operation in the same transaction
 No data is affected until all locks are obtained—that is, until the transaction is in its locked
point.

You might also like