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

Chapter 2

Transactions Management in DBMS


Transaction Management (TM)
• A transaction is a group of tasks.
• A transaction is a set of logically related operations.
• A transaction is implemented by a computer program that includes
database commands like retrievals, insertions, deletions, and updates.
• For example, you are transferring money from your bank account to your
friend’s account, the set of operations.
• A single task is the minimum processing unit which cannot be divided
further.
• Let’s take 2 examples of a simple transaction.
1. Suppose a bank employee transfers 500 ETB from A's account to B's
account.
2
…cont.

2. Simple Transaction Example


1. Read your account balance • This whole set of operations can be
2. Deduct the amount from your balance called a transaction.
3. Write the remaining balance to your account • There is read, write and update
operations in the above example but
4. Read your friend’s account balance
the transaction can have operations
5. Add the amount to his account balance like read, write, insert, update, delete.
6. Write the new updated balance to his account
3
..cont.

• In DBMS, we write the above 6 steps transaction like


below:
• Lets say your account is A and your friend’s account is B,
you are transferring 10,000 ETB from A to B, the steps of
the transaction are:
1. R(A);
2. A = A – 10,000;
3. W(A);
4. R(B);
5. B = B + 10,000;
4
6. W(B);
ACID Properties in DBMS

• A transaction is a single logical unit of work which


accesses and possibly modifies the contents of a
database.
• Transactions access data using read and write
operations.
• In order to maintain consistency in a database, before and
after the transaction, certain properties are followed.
• These are called ACID properties.

5
…cont.

Atomicity
• This means that either the entire transaction
takes place at once or doesn’t happen at all.
• There is no midway i.e. 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 database are not visible.
—Commit: If a transaction commits,
changes made are visible.
• Atomicity is also known as the ‘All or nothing
rule’.
6
…cont.

• Consider the following transaction T consisting of T1 and T2:


Transfer of 100 ETB from account X to account Y

• If the transaction fails after completion of T1 but before completion of T2.( say, after write(X)
but before write(Y)), then amount has been deducted from X but not added to Y.
• This results in an inconsistent database state.
• Therefore, the transaction must be executed in entirety in order to ensure correctness of
database state.

7
…cont.

• Consistency
– The integrity constraints must be maintained so that the
database is consistent before and after the transaction.
– It refers to the correctness of a database.
– Referring to the example above, the total amount before and
after the transaction must be maintained.
– Total before T occurs = 500 + 200 = 700.
– Total after T occurs = 400 + 300 = 700
– Therefore, database is consistent.
– Inconsistency occurs in case T1 completes but T2 fails.
– As a result T is incomplete. 8
…cont.

• Isolation
– Transactions occur independently without interference.
– In a database system if more than one transaction are being executed
simultaneously and in parallel,
– the property of isolation states that all the transactions will be carried out and
executed as if it is the only transaction in the system.
– No transaction will affect the existence of any other transaction.

9
…cont.

• Suppose T has been executed till Read (Y) and then T’’ starts.
• As a result , interleaving of operations takes place due to which
T’’ reads correct value of X but incorrect value of Y and sum
computed by:
• T’’: (X+Y = 50,000+500=50,500) is thus not consistent with the
sum at end of transaction: T: (X+Y = 50,000 + 450 = 50,450).
• This results in database inconsistency, due to a loss of 50 units.
• Hence, transactions must take place in isolation and changes
should be visible only after they have been made to the main
memory. 10
…cont.

• Durability: it 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.
• Effects of a correctly terminated transaction should persist
over time
– The DBMS protects the DB against failures

11
Types of failures and Transaction States
• Failures are classified as transaction, system, and media failures.
• There are several possible reasons for a transaction to fail in the middle of
execution:
– A computer failure (system crash)
• A hardware, software, or network error occurs in the computer system
during transaction execution.
• Hardware crashes are usually media failures—for example, main memory
failure
– A transaction or system error
• integer overflow or division by zero
• because of erroneous parameter values or because of a logical programming error
• the user may interrupt the transaction during its execution.

12
…cont.
– Local errors or exception conditions detected by the
transaction
• Certain conditions may occur that necessitate cancellation of the
transaction
• For example, data for the transaction may not be found
• Insufficient account balance in a banking database, cause a
transaction, such as a fund withdrawal, to be canceled
– Concurrency control enforcement
• It may abort a transaction because it violates serializability
• Or it may abort one or more transactions to resolve a state of deadlock
among several transactions.

13
..cont.
– Disk failure.
• Some disk blocks may lose their data because of a read or write
malfunction or because of a disk read/write head crash.
• This may happen during a read or a write operation of the transaction
– Physical problems and catastrophes
• The endless list of problems that includes power or air-conditioning
failure, fire, theft, sabotage, overwriting disks or tapes by mistake, and
mounting of a wrong tape by the operator.

14
Transaction States

• A transaction is a unit of database processing which contains


a set of operations.
• For example, deposit of money, balance enquiry, reservation
of tickets etc.
• Every transaction starts with delimiters begin transaction and
terminates with end transaction delimiters.
• The set of operations within these two delimiters constitute
one transaction.
• A transaction is divided into states to handle various situations
such as failure. 15
..cont.

• The state of a transaction is defined by the current activity


it is performing.
• At a particular instant of time, a transaction can be in one
of the following state:
– Active − Transaction is executing.
– Failed − Transaction fails to complete successfully.
– Abort − changes made by transaction are cancelled (roll back).
– Partially commit − Final statement of transaction is executed.
– Commit − Transaction completes its execution successfully.
– Terminated − Transaction is finished.
16
…cont.

A transaction will terminate either when it commits or


when it is aborted

Representation of states of transaction diagrammatically

17
Concurrent Execution of Transactions and its
Advantages
• Concurrency is the ability to allow multiple users to affect
multiple transaction within a database.
• If many transactions try to access the same data, then
inconsistency arises.
• Concurrency control required to maintain
consistency data.
• For example, if we take ATM machines and do not use
concurrency, multiple persons cannot draw money at a
time in different places
18
Concurrent Execution in DBMS

• In a multi-user system, multiple users can access and use


the same database at one time
• It is known as the concurrent execution of the database.
• It means that the same database is executed
simultaneously on a multi-user system by different users.

19
…cont.

• Concurrent execution: executing transactions


simultaneously has the following advantages:
– Increased processor and disk utilization, leading to better
throughput
– One transaction can be using the CPU while another is reading
from or writing to the disk
– Reduced average response time for transactions: short
transactions need not wait behind long ones

20
Need for Concurrency Control in DBMS

• Most high-performance transactional systems need to run


transactions concurrently to meet their performance
requirements.

• Thus, without concurrency control systems can neither provide


correct results nor maintain their databases consistently
• In general, Concurrency Control is:
– To enforce Isolation among conflicting transactions.
– To preserve database consistency through consistency preserving execution of
transactions.
– To resolve read-write and write-write conflicts.
21
…cont.

Concurrency control Techniques:


1. Locking
2. Timestamp
3. Optimistic
4. Multi-version and
5. Lock granularity
22
Locking Techniques for Concurrency Control

• Some of the main techniques used to control concurrent execution of


transactions is locking data items
• A lock is a variable associated with a data item that describes the status of
the item with respect to possible operations that can be applied to it

23
Query Processing in DBMS and Query Optimization
• This section discusses the techniques used internally by a DBMS to
process, optimize, and execute high-level queries.
• Query: A query is a request for information from a database.
• A query expressed in a high-level query language such as SQL must
first be scanned, parsed, and validated.
Steps of query processing
• The scanner identifies the query tokens like SQL keywords, attribute
names, and relation names that appear in the text of the query.
• Parser checks the query syntax to determine whether it is formulated
according to the syntax rules of the query language.

24
…cont.

• Validation- the query must be validated by checking all


attribute and relation names are valid and semantically
meaningful names in the schema of the particular database
being queried.
• The DBMS must devise an execution strategy or query plan
for retrieving the results of the query from the database files.
• A query has many possible execution strategies
• The process of choosing a suitable one for processing a
query is known as query optimization.
25
…Cont.

• The query optimizer module has the task of producing a good


execution plan, and
• The code generator generates the code to execute that plan.
• The runtime database processor has the task of running
(executing) the query code, to produce the query result.
• If a runtime error results, an error message is generated by the
runtime database processor.
• See the diagram on next slide.

26
…cont.

Typical steps when processing a high-level query

27
28

You might also like