Language OfDbaba

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

COURSE TITLE: DATABASE DESIGN AND MANAGEMENT

COURSE CODE: CSC 303

Topic: Introduction to Transaction Management and Concurrency Control


A transaction is a single logical unit that accesses and modifies the contents of the database. The logical
unit of work that must be entirely completed or aborted. Transactions access data using read-and-write
operations.
A transaction is a group of query commands intended for spontaneous accomplishment in a consistent
perspective of a database. It is a sequence of database commands; disk reads and writes. It is also referred
to as orderly processing analogous to a succession of preliminary physical operations (reads/writes) on the
database. SQL code represents a transaction because it accesses a database. Transaction is a single
operation of processing that can have many operations.
When the data of users is stored in a database, that data needs to be accessed and modified from time to
time. This task should be performed with a specified set of rules and in a systematic way to maintain the
consistency and integrity of the data present in a database. In DBMS, this task is called a transaction.
Improper or incomplete transactions can have devastating effects on database integrity. Transaction Log
Keeps track of all transactions that update the database.
A transaction consists of:
SELECT statement
Series of related UPDATE statements
Series of INSERT statements
Combination of SELECT, UPDATE, and INSERT statements

ACID Properties of Transaction:


ACID stands for Atomicity, Consistency, Isolation and Durability
Atomicity: All the operations in a transaction are considered to be atomic and as one unit. If system fails
or any read/write conflicts occur during transaction the system needs to revert back to its previous state.
Atomicity is maintained by the Transaction Management Component.
Consistency: Every transaction should lead to database connection from one valid state to other valid
state. If system fails because of invalid data while doing an operation revert back the system to its
previous state. Consistency is maintained by the Application manager.
Isolation: If multiple transactions are executing on single database, each transaction should be isolated
from other transaction. If multiple transactions are performed on single database, operation from any
transaction should not interfere with operation in other transaction. Isolation is maintained by the
concurrency control manager.
Durability: Durability means the changes made during the transactions should exist after completion of
transaction. Changes must be permanent and must not be lost due to any database failure. It is maintained
by the recovery manager.

Example:
A has an account with an amount of #150. B has an account with an amount of #50. A is transferring
amount #100 to B’s account.
Atomicity: Operations required for transfer are: Deduct amount #100 from A’s account. Add amount
#100 to B’s account. All operations should be done. If system fails to add amount in B’s account after
deducting from A’s account, revert the operation on A’s account.
Consistency: The sum amount in A’s account and B’s account should be same before and after the
transaction completes. In the example the sum of both account before and after transaction is #200, which
preserves the consistency.
Isolation: If there is any other transaction (let between A and C) is going on, it should not affect the
transaction between A and B i.e., both the transactions should be isolated.
Durability: It may happen system gets crashed after the completion of all operations then, after restarting
it should preserve all the changes. The amount in A’s and B’s account should be same before and after
the system restart.

Why is transaction necessary in databases?


• Databases are shared resources: A database is a shared resource accessed by many users and processes
concurrently.
• To guide against a problem: There will be issues if this concurrent access to a common resource is not
managed properly (not unlike in operating systems). In the absence of transaction in databases, the
following can occur: i. Concurrent-related issues ii. Failure-related issues
• The most significant benefit of transferring data is data confidentiality. Many database applications require
storing data in several tables or multiple rows in the same table to maintain a consistent data set.
• If transactions are used, other connections to the same database can either see all of the changes or none
at all.
THE BASIC FORM OF AN SQL QUERY IS AS FOLLOWS:
Every query must have a SELECT clause, which specifies columns to be retained in the result, and a FROM
clause, which specifies a cross product of tables. The optional WHERE clause specifies selection conditions
on the tables mentioned in the FROM clause. Let us consider a simple query.
SELECT DISTINCT S.sname, S.age
FROM Sailors S;

The answer is a set of rows, each of which is a pair 〈 sname, age 〉. If two or more sailors have the same
name and age, the answer still contains just one pair with that name and age.

The answer to this query with and without the keyword DISTINCT on instance S3 of Sailors is shown in
Figures 1 and 2. The only difference is that the tuple for Horatio appears twice if DISTINCT is omitted;
this is because there are two sailors called Horatio and age 35.
Figure1: Query with DISTINCT keyword Figure 2: Query without DISTINCT keyword

SELECT S.sid, S.sname, S.rating, S.age


FROM Sailors AS S
WHERE S.rating > 7;

SELECT *
FROM Sailors;

This query will retrieve all the column in the Sailors table.
Find the age of the youngest sailor who is eligible to vote (i.e., is at least 18 years old) for each rating level
with at least two such sailors.
SELECT S.rating, MIN (S.age) AS minage
FROM Sailors S
WHERE S.age >= 18

Transaction Management in SQL


Following is the commands used for transaction control:
COMMIT: The COMMIT command is used to save changes made by the transaction in the database.
This is when all operations in a database are successfully concluded, then; you can permanently commit
the changes to the database.
Syntax:
COMMIT;
ROLLBACK: The ROLLBACK command is used to undo saved changes made by the transaction in the
database.
Syntax:
ROLLBACK;
SAVEPOINT: SAVEPOINT is a point in the transaction where transaction can be rolled back without
entire transaction rollback. Perhaps any of the operations fails, then; you can roll back all the changes
executed by previous operations.
Syntax:
savepoint_name;

CONCURRENT EXECUTIONS
Concurrency in Database Management Systems (DBMS) is a critical concept that enables multiple
transactions to be executed simultaneously without compromising data integrity and consistency. This
capability is essential in environments where numerous users or applications need to access and modify the
database concurrently. Multiple transactions are allowed to run concurrently in the system.
Advantages are:
1) Increased processor and disk utilization, leading to better transaction throughput e.g. one transaction can
be using the CPU while another is reading from or writing to the disk.
2) Reduced average response time for transactions: short transactions need not wait behind long ones.
Concurrency control schemes – mechanisms to achieve isolation
Concurrency control schemes is, to control the interaction among the concurrent transactions in order to
prevent them from destroying the consistency of the database.
Concurrency control is vital for several reasons:

1) Database Consistency: It ensures that the database remains consistent even when multiple
transactions are executed simultaneously. Without proper control, conflicting updates could lead
to an inconsistent state.
2) Avoiding Conflicts: Concurrency control prevents lost updates, where one transaction's changes
may overwrite another's without proper coordination. This is crucial in scenarios like banking,
where simultaneous withdrawals could lead to incorrect balances.
3) Dirty Reads Prevention: It stops transactions from reading uncommitted changes made by
others, which can lead to inaccurate data being processed.
4) System Efficiency: By managing concurrent access, DBMS can enhance throughput and
optimize resource utilization, allowing for faster transaction processing.
Common Problems in Concurrency
While concurrency improves performance, it also introduces several challenges:
1) Lost Updates: Occurs when two transactions read the same data and one updates it,
potentially overwriting the other’s changes.
2) Dirty Reads: Happens when a transaction reads data modified by another transaction that has
not yet committed, leading to potential inaccuracies.
3) Phantom Reads: Arises when a transaction retrieves a set of records and another transaction
modifies the dataset before the first transaction completes, resulting in different results upon
re-querying.
Concurrency Control Techniques
To address these challenges, various concurrency control techniques are employed:

1) Two-Phase Locking (2PL): This protocol divides transactions into two phases—growing (where
locks are acquired) and shrinking (where locks are released). It ensures serializability but can lead
to deadlocks if not managed properly.
2) Timestamp Ordering Protocol: Transactions are ordered based on their timestamps. Older
transactions have higher priority, which helps maintain consistency by ensuring that operations
occur in a predetermined order.
3) Multi-Version Concurrency Control (MVCC): This technique allows multiple versions of a
data item to exist simultaneously. It enables readers to access a snapshot of the database while
writers make changes, thus reducing conflicts between read and write operations.
4) Validation Concurrency Control: This method checks whether a transaction can be committed
only at the end of its execution based on the state of the database at that time, ensuring that no
conflicting operations have occurred during its execution

Schedule
Schedule is process of grouping transactions into one and executing them in a predefined order. It is a
sequence of execution of operation from various transactions. Schedule is required in database because
when multiple transactions execute in parallel, they may affect the result of each other. So, to resolve this
the order of the transactions are changed by creating a schedule.
Types of Schedules:
Serial Schedule: A schedule in which the transactions are defined to execute one after another is called
serial schedule.
Non- Serial Schedule: A schedule in which the transactions are defined to execute in any order is called
non-serial schedule.
Recoverable and Non-Recoverable Schedule
Schedules in which transactions commit only after all transactions whose changes they commit are called
recoverable schedule.
If some transaction Tj is reading value updated or written by some other transaction T i then, the commit
of Tj must occur after the commit of Ti.
Schedule S1 Schedule S2

T1 T2 T1 T2

R(X)
R(X)
X=X+10
X=X+10
W(X)
W(X)
COMMIT

R(X) R(X)
Rollback X=X-5 X=X-5
COMMIT W(X) W(X)
COMMIT COMMIT

Non-Recoverable Schedule Recoverable Schedule

Cascading Abort and Cascadeless Schedule


If in a schedule, failure of one transaction causes rollback or abort to other dependent transaction such a
schedule is called Cascading abort. It leads to wastage of CPU time.
If in a schedule, a transaction is not allowed to read a data item until the last transaction that has written
it is committed or aborted, then such a schedule is called Cascadeless schedule.
Strict Schedule
If in a schedule, a transaction is neither allowed to read nor write a data item until the last transaction
that has written it is committed or aborted, then such schedule is called strict schedule.
Strict schedule implements more restrictions than cascadeless schedule.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy