Concurrency Control Protocols
Concurrency Control Protocols
Transactions
UNIT 3
Query Processing
• During this process, Parser checks the syntax and verifies the relations and the
attributes which are used in the query.
Query Optimization
• It is the process of selecting the most efficient query evaluation plan from
among various strategies if the query is complex.
Example
SELECT Ename FROM Employee
WHERE Salary > 5000;
OR
Partially Committed :
• After completion of all the read and write operation the changes are
made in main memory or local buffer. If the changes are made
permanent on the DataBase then the state will change to “committed
state” and in case of failure it will go to the “failed state”.
Failed State :
• When any instruction of the transaction fails, it goes to the “failed
state” or if failure occurs in making a permanent change of data on
Data Base.
Aborted State :
• After having any type of failure the transaction goes from “failed
state” to “aborted state” and since in previous states, the changes are
only made to local buffer or main memory and hence these changes
are deleted or rolled-back.
Committed State:
• It is the state when the changes are made permanent on the Data Base
and the transaction is complete and therefore terminated in the
“terminated state”.
Terminated State:
• If there isn’t any roll-back or the transaction comes from the
“committed state”, then the system is consistent and ready for new
transaction and the old transaction is terminated.
a.Atomicity
• Transactions do not occur partially.
• Each transaction is considered as one unit and either runs to
completion or is not executed at all.
• It involves the following two operations.
—Abort: If a transaction aborts, changes made to the database are not
visible.
—Commit: If a transaction commits, changes made are visible.
Atomicity is also known as the ‘All or nothing rule’.
Consider the following transaction T consisting of T1 and T2: Transfer
of 100 from account X to account Y.
This results in database inconsistency. Hence, transactions must take place in isolation and
changes should be visible only after they have been made to the main memory.
d.Durability
• This property ensures that once the transaction has completed execution.
• The updates and modifications to the database are stored in and written to
disk and they persist even if a system failure occurs.
transactions which may result in delay in the overall execution. Hence for increasing
the overall throughput and efficiency of the system, several transactions are
executed.
data .
Example
• Imagine a library database where users can borrow and return books. Two users,
Alice and Bob, both want to borrow the same book from the library at the same time.
Step 1: Alice checks the availability of the book and finds that it's available.
Step 2: The database locks the book to prevent other users from borrowing it.
Step 3: Bob also checks the availability of the book but is unable to borrow it because
it's already locked by Alice's transaction.
Step 4: After Alice returns the book, the lock is released, and Bob can then borrow it.
Step 5: Both transactions complete successfully without conflicts, and the database
remains consistent.
Concurrency Control Problems
• The database transaction consist of two major operations “Read” and “Write”.
Problems:
Example:
• Consider two transactions TX and TY in the below diagram performing read/write
operations on account A where the available balance in account A is $300:
At time t1, transaction TX reads the value of Then at time t4, transaction TY reads account A
account A, i.e., $300. that will be read as $350.
At time t2, transaction TX adds $50 to Then at time t5, transaction TX rollbacks due to
account A that becomes $350. server problem, and the value changes back to
$300 (as initially).
At time t3, transaction TX writes the updated
value in account A, i.e., $350. But the value for account A remains $350 for
transaction TY as committed.
2. Lost Update Problem (W-W Conflict)
The problem occurs when,
i.Two different database transactions perform the read/write operations on the same
database items in concurrent execution.
ii.That makes the values of the items incorrect hence making the database inconsistent.
iii. Hence, there comes the Lost Update Problem.
Example:
Consider the below diagram where two transactions TX and TY, are performed on the
same account A where the balance of account A is $300.
i. At time t1, transaction TX reads the value of v. At time t6, transaction TX writes the
account A, i.e., $300 (only read). value of account A that will be updated as
ii. At time t2, transaction TX deducts $50 from $250 only, as TY didn’t update the value
account A that becomes $250 (only deducted and not yet.
updated/write). vii. Similarly, at time t7, transaction TY
iii. Alternately, at time t3, transaction TY reads the writes the values of account A, so it will
value of account A that will be $300 only because TX write as done at time t4 that will be $400. It
didn't update the value yet. means the value written by TX is lost, i.e.,
iv. At time t4, transaction TY adds $100 to account $250 is lost.
A that becomes $400 (only added but not
updated/write).
3. Unrepeatable Read Problem (W-R Conflict)
• Also known as Inconsistent Retrievals Problem that occurs when in a transaction, two
different values are read for the same database item.
Example:
• Consider two transactions, TX and TY, performing the read/write operations on
account A, having an available balance = $300. The diagram is shown below:
• At time t1, transaction TX reads the value from account A, i.e., $300.
• At time t2, transaction TY reads the value from account A, i.e., $300.
• At time t3, transaction TY updates the value of account A by adding $100 to the
available balance, and then it becomes $400.
• At time t4, transaction TY writes the updated value, i.e., $400.
• After that, at time t5, transaction TX reads the available value of account A, and that
will be read as $400.
• It means that within the same transaction TX, it reads two different values of account
A,
• i.e., $ 300 initially, and after updation made by transaction TY, it reads $400. It is an
unrepeatable read and is therefore known as the Unrepeatable read problem.
Concurrency Control Protocols
• Concurrency Control is the working concept that is required for controlling and
managing the concurrent execution of database operations and thus avoiding the
inconsistencies in the database.
• Thus, for maintaining the concurrency of the database, we have the concurrency
control protocols.
• These protocols use locks to regulate access to data items, granting exclusive access
to a data item to one transaction at a time.
• Transactions requesting to read or write a data item acquire a lock on that item, while
other transactions attempting to access the same item must wait until the lock is
released.
1.1 Shared Lock (S-lock): Also known as a read lock, a shared lock
allows multiple transactions to read a data item simultaneously.
However, it prevents any transaction from writing to the data
item until all shared locks are released.
• Main Memory Buffer: When transactions are executed, their changes are
temporarily stored in a main memory buffer. This buffer acts as a staging area
before the changes are written to disk.
• Disk Storage (Logs): The changes from the main memory buffer are then
written to disk in the form of logs. These logs record the sequence of
operations performed by transactions, including updates, inserts, and deletes.
• Database: The actual database resides on disk. It contains the stored data as
well as metadata about the database structure.
a. Rollback/Undo Recovery Technique
• The rollback/undo recovery technique is based on the principle of backing out or
undoing the effects of a transaction that has not been completed successfully due to a
system failure or error.
• This technique is accomplished by undoing the changes made by the transaction using
the log records stored in the transaction log.
• The transaction log contains a record of all the transactions that have been
performed on the database.
• The system uses the log records to undo the changes made by the failed transaction
and restore the database to its previous state.
b.Commit/Redo Recovery Technique
• The commit/redo recovery technique is based on the principle of reapplying the
changes made by a transaction that has been completed successfully to the
database.
• This technique is accomplished by using the log records stored in the transaction log
to redo the changes made by the transaction that was in progress at the time of the
failure or error.
• The system uses the log records to reapply the changes made by the transaction and
restore the database to its most recent consistent state.
c.Checkpoint Recovery
• In the event of a failure, the system can use the checkpoint file to restore
the database to the most recent consistent state before the failure
occurred, rather than going through the entire log to recover the database.
The Log Information looks as follows:
a. Undoing
b. Deferred Update
c. Immediate Update
d. Caching/Buffering
e. Shadow Paging
f. Backward Recovery
g. Forward Recovery
a. Undoing
• This technique does not physically update the database on disk until a transaction
has reached its commit point.
• Before reaching commit, all transaction updates are recorded in the local
transaction workspace.
• If a transaction fails before reaching its commit point, it will not have changed
the database in any way so UNDO is not needed.
• It may be necessary to REDO the effect of the operations that are recorded in the
local transaction workspace, because their effect may not yet have been written in
the database.
• Hence, a deferred update is also known as the No-undo algorithm.
c.Immediate Update
• One or more disk pages that include data items to be updated are
cached into main memory buffers and then updated in memory before
being written back to disk.
• A collection of in-memory buffers called the DBMS cache is kept
under the control of DBMS for holding these buffers.
• A directory is used to keep track of which database items are in the
buffer.
e. Shadow Paging
• Imagine you have a book that you're constantly updating. Instead of erasing and
rewriting pages every time there's a change, you create a copy of the entire book with
the changes. This copy is your "shadow." When everything is updated and ready, you
swap the old book with the new one.
• In databases, this translates to keeping a shadow copy of the database. When changes
are made, they go to this shadow copy first.
• If something goes wrong during an update, you can revert to the last consistent state
by simply discarding the changes in the shadow copy and sticking with the original
database.
• This helps ensure data integrity during recovery processes.
f.Backward Recovery: It involves restoring a database to a previously
consistent state before a failure occurred, typically using techniques like rolling
back transactions or restoring from backups. This helps recover from failures by
undoing changes that led to the inconsistency.