Unit4 Transaction Part 1
Unit4 Transaction Part 1
Tech
Course Name: Database Management System
Course Code:E2UC302B
Transaction in DBMS
DBMS stands for Database Management System, which is a tool or software
used to create, delete, or manipulate the current database.
Any logical work or set of works that are done on the data of a database is
known as a transaction.
Logical work can be inserting a new value in the current database, deleting
existing values, or updating the current values in the database.
What does a Transaction mean in DBMS?
Transaction in Database Management Systems (DBMS) can be
defined as a set of logically related operations.
It also has some specific properties that must be followed to keep the
database consistent.
Example of Transaction
• In DBMS, we write the above 6 steps transaction like this:
• Lets say your account is A and your friend’s account is B, you are transferring
1000 from A to B, the steps of the transaction are:
• 1. R(A);
• 2. A = A - 10000;
• 3. W(A);
• 4. R(B);
• 5. B = B + 10000;
• 6. W(B);
• In the above transaction R refers to the Read operation and W refers to the
write operation.
Example of Transaction
To complete a transaction, we have to follow some steps which make a transaction successful. For
example, we withdraw the cash from ATM is an example of a transaction, and it can be done in the
following steps:
• Initialization if transaction
• Inserting the ATM card into the machine
• Choosing the language
• Choosing the account type
• Entering the cash amount
• Entering the pin
• Collecting the cash
• Aborting the transaction
• So, in the same way, we have three steps in the DBMS for a transaction which are the
following:
• Read Data
• Write Data
• Commit
Operations of Transactions
A user can make different types of requests to access and modify the
contents of a database. So, we have different types of operations
relating to a transaction. They are discussed as follows:
(i) Read(X) A read operation is used to read the value of X from
the database and store it in a buffer in the main memory for further
actions such as displaying that value.
To ensure that further operations of any other transaction are performed only after work of
the current transaction is done, a commit operation is performed to the changes made by a
transaction permanently to the database.
iv) Rollback This operation is performed to bring the database to the last saved state when
any transaction is interrupted in between due to any power, hardware, or software failure.
In simple words, it can be said that a rollback operation does undo the operations of
transactions that were performed before its interruption to achieve a safe state of the
database and avoid any kind of ambiguity or inconsistency.
Transaction property
ACID Properties
There are four properties of a transaction that should be maintained during the
transaction.
Atomicity: It means either a transaction will take place, or it will fail. There
will not be any middle state like partial completion.
Consistency: The database should be consistent before and after the
transaction. Correctness and integrity constraints should be maintained during
the transaction.
Isolation: This property means multiple transactions can occur at the same
time without affecting each other. If one transaction is occurring, then it should
not bring any changes in the data for the other transaction, which is occurring
concurrently.
Durability: It means if there is a successful transaction, then all changes
should be permanent, so if there is any system failure, we will be able to
retrieve the updated data.
States of Transaction
Active state In this state, the transaction is being executed. 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 In the partially committed state, a transaction executes its final operation,
but the data is still not saved to the database. In the total mark calculation example, a final display
of the total marks step is executed in this state.
3. Final Write
A final write must be the same between both the schedules. In schedule S1, if a transaction T1 updates
A at last then in S2, final writes operations should also be done by T1.
Recoverability of Schedule and Irrecoverable Schedule : Example
Sometimes a transaction may not execute completely due to a software issue,
system crash or hardware failure. In that case, the failed transaction has to be
rollback. But some other transaction may also have used value produced by the
failed transaction. So we also have to rollback those transactions.
Testing of Serializability
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.
Problems with Concurrent Execution : Lost Update Problems (W - W Conflict)
At time t1, transaction TX reads the value of account A, i.e., $300 (only read).
At time t2, transaction TX deducts $50 from account A that becomes $250 (only deducted and
not updated/write).
Alternately, at time t3, transaction TY reads the value of account A that will be $300 only
because TX didn't update the value yet.
At time t4, transaction TY adds $100 to account A that becomes $400 (only added but not
updated/write).
At time t6, transaction TX writes the value of account A that will be updated as $250 only, as
TY didn't update the value yet.
Similarly, at time t7, transaction TY writes the values of account A, so it will write as done at
time t4 that will be $400. It means the value written by TX is lost, i.e., $250 is lost.
Hence data becomes incorrect, and database sets to inconsistent.
Dirty Read Problems (W-R Conflict)
The dirty read problem occurs when one transaction updates an item of the database, and
somehow the transaction fails, and before the data gets rollback, the updated database item is
accessed by another transaction. There comes the Read-Write Conflict between both
transactions.
• For 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:
Dirty Read Problems (W-R Conflict)
At time t1, transaction TX reads the value of account A, i.e., $300.
At time t2, transaction TX adds $50 to account A that becomes $350.
At time t3, transaction TX writes the updated value in account A, i.e.,
$350.
Then at time t4, transaction TY reads account A that will be read as $350.
Then at time t5, transaction TX rollbacks due to server problem, and the
value changes back to $300 (as initially).
But the value for account A remains $350 for transaction T Y as committed,
which is the dirty read and therefore known as the Dirty Read Problem.
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.
• For 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 T Y, it
reads $400. It is an unrepeatable read and is therefore known as the
Unrepeatable read problem.