UNIT-5 notes[1]
UNIT-5 notes[1]
1. WHAT IS TRANSACTION?
The transaction is a set of logically related operation. It contains
a group of tasks.
A transaction is an action or series of actions. It is performed by
a single user to perform operations for accessing the contents of
the database.
Example: Suppose an employee of bank transfers Rs 800 from
X's account to Y's account. This small transaction contains
several low-level tasks:
X's Account :-
Open_Account(X)
Old_Balance = X.balance
New_Balance = Old_Balance - 800
X.balance = New_Balance
Close_Account(X)
Y's Account :-
Open_Account(Y)
Old_Balance = Y.balance
New_Balance = Old_Balance + 800
Y.balance = New_Balance
Close_Account(Y)
2. OPERATION IN TRANSACTION?
Read(X): Read operation is used to read the value of X from
the database and stores it in a buffer in main memory.
Write(X): Write operation is used to write the value back to the
database from the buffer.
Atomicity
o ○ The phrase “all or nothing” describes the first ACID property
i.e. atomicity.
o It states that all operations of the transaction take place at once
if not, the transaction is aborted.
o There is no midway, i.e., the transaction cannot occur partially.
Each transaction is treated as one unit and either run to
completion or is not executed at all.
o Atomicity involves the following two
operations:
o Abort: If a transaction aborts then all the changes
made are not visible.
o Commit: If a transaction commits then all the
changes made are visible.
Read(A) Read(B)
A:= A-100 Y:= Y+100
Write(A) Write(B)
Consistency
o The phrase “no violation of integrity constraints” describes the
property of consistency.
o The integrity constraints are maintained so that the database is
consistent before and after the transaction.
o The execution of a transaction will leave a database in either its
prior stable state or a new stable state.
o The consistent property of database states that every
transaction sees a consistent database instance.
o The transaction is used to transform the database from one
consistent state to another consistent state.
For example: The total amount must be maintained before or after
the transaction.
1. Total before T occurs = 600+300=900
2. Total after T occurs= 500+400=900
Therefore, the database is consistent. In the case when T1 is
completed but T2 fails, then inconsistency will occur.
Without a consistent property, money can be credited or debited by
the transaction. It is the responsibility of the application
programmers who code the transaction to maintain the consistency
for individual transactions by enforcing consistency constraints on
the database.
Isolation
o It shows that the data which is used at the time of execution of
a transaction cannot be used by the second transaction until
the first one is completed.
o In isolation, if the transaction T1 is being executed and using
the data item X, then that data item can't be accessed by any
other transaction T2 until the transaction T1 ends.
o The concurrency control subsystem of the DBMS enforced the
isolation property.
For example:
Let X = 500, Y = 500.
Consider two transactions T and T”.
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
the correct value of X but the 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 the 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.
Durability
o The durability property is used to indicate the performance of
the database's consistent state. It states that the transaction
made the permanent changes.
o They cannot be lost by the erroneous operation of a faulty
transaction or by the system failure. When a transaction is
completed, then the database reaches a state known as the
consistent state. That consistent state cannot be lost, even in
the event of a system's failure.
o The recovery subsystem of the DBMS has the responsibility of
Durability property.
Some important points:
Consistenc
Application programmer
y
4. TRANSACTION STATES?
Active state
o The active state is the first state of every transaction. In this
state, the transaction is being executed.
o For example: Insertion or deletion or updating a record is done
here. But all the records are still not saved to the database.
Partially committed
o In the partially committed state, a transaction executes its final
operation, but the data is still not saved to the database.
o In the total mark calculation example, a final display of the total
marks step is executed in this state.
Committed
A transaction is said to be in a committed state if it executes all its
operations successfully. In this state, all the effects are now
permanently saved on the database system.
Failed state
o If any of the checks made by the database recovery system fails,
then the transaction is said to be in the failed state.
o In the example of total mark calculation, if the database is not
able to fire a query to fetch the marks, then the transaction will
fail to execute.
Aborted
o If any of the checks fail and the transaction has reached a failed
state then the database recovery system will make sure that the
database is in its previous consistent state. If not then it will
abort or roll back the transaction to bring the database into a
consistent state.
o If the transaction fails in the middle of the transaction then
before executing the transaction, all the executed transactions
are rolled back to its consistent state.
o After aborting the transaction, the database recovery module
will select one of the two operations:
o Re-start the transaction
o Kill the transaction
5. Schedule
A series of operation from one transaction to another transaction is
known as schedule. It is used to preserve the order of the operation
in each of the individual transaction.
1. Serial Schedule
The serial schedule is a type of schedule where one transaction is
executed completely before starting another transaction. In the serial
schedule, when the first transaction completes its cycle, then the
next transaction is executed.
2. Non-serial Schedule
o If interleaving of operations is allowed, then there will be non-
serial schedule.
o It contains many possible orders in which the system can
execute the individual operations of the transactions.
o In the given figure (c) and (d), Schedule C and Schedule D are
the non-serial schedules. It has interleaving of operations.
3. Serializable schedule
o The serializability of schedules is used to find non-serial
schedules that allow the transaction to execute concurrently
without interfering with one another.
o It identifies which schedules are correct when executions of the
transaction have interleaving of their operations.
o A non-serial schedule will be serializable if its result is equal to
the result of its transactions executed serially.
Conflict Serializable: A schedule is called conflict serializable if it
can be transformed into a serial schedule by swapping non-
conflicting operations. Two operations are said to be conflicting
if all conditions satisfy:
Conflicting Operations:-
For example:-
6. Concurrency Control
Concurrency Control is the management procedure that is required
for controlling concurrent execution of the operations that take place
on a database.
But before knowing about concurrency control, we should know
about concurrent execution.
(OR) Concurrency Control
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.
2. Exclusive lock:
o In the exclusive lock, the data item can be both reads as well as
written by the transaction.
o This lock is exclusive, and in this lock, multiple transactions do
not modify the same data simultaneously.
o Definition: An exclusive lock allows only one transaction to
access a resource for modification and prevents any other
transaction from reading or modifying it.
o Purpose: Ensures data consistency during write operations by
blocking all other transactions from accessing the locked
resource.
o Example: If Transaction A places an exclusive lock on a record
to update it, no other transaction can read or modify that record
until the exclusive lock is released.
For example:
Scenario: *Online Ticket Booking System*
Imagine a movie ticket booking platform where multiple users are
trying to book tickets for the same show at the same time. Each show
has a limited number of seats available.
| ShowID | AvailableSeats |
|------------|-------------------|
| 101 |5 |
With Lock:
The DBMS uses locking to ensure data consistency:
ADDITIONAL :-
List out the transaction properties ?
Mention the operation in transaction?