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

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:
What is a Database Transaction?
• A Database Transaction is a logical unit of processing in a DBMS which
entails one or more database access operation. In a nutshell, database
transactions represent real-world events of any enterprise.
• All types of database access operation which are held between the
beginning and end transaction statements are considered as a single
logical transaction in DBMS. During the transaction the database is
inconsistent. Only once the database is committed the state is changed
from one consistent state to another.
• Facts about Database Transactions
• A transaction is a program unit whose execution may or may not change
the contents of a database.
• The transaction concept in DBMS is executed as a single unit.
• If the database operations do not update the database but only retrieve
data, this type of transaction is called a read-only transaction.
• A successful transaction can change the database from one CONSISTENT
STATE to another
• DBMS transactions must be atomic, consistent, isolated and durable
• If the database were in an inconsistent state before a transaction, it would
remain in the inconsistent state after the transaction.
Operations of Transaction:

• Following are the main operations of 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.
• Let's take an example to debit transaction from an account which consists of following operations:
• 1. R(X);
• 2. X = X - 500;
• 3. W(X);
• Let's assume the value of X before starting of the transaction is 4000.
• The first operation reads X's value from database and stores it in a buffer.
• The second operation will decrease the value of X by 500. So buffer will contain 3500.
• The third operation will write the buffer's value to the database. So X's final value will be 3500.
• But it may be possible that because of the failure of hardware, software or power, etc. that
transaction may fail before finished all the operations in the set.
• For example: If in the above transaction, the debit transaction fails after executing operation 2
then X's value will remain 4000 in the database which is not acceptable by the bank.
• To solve this problem, we have two important operations:
• Commit: It is used to save the work done permanently.
• Rollback: It is used to undo the work done.
• Why do you need concurrency in Transactions?
• A database is a shared resource accessed. It is used by many
users and processes concurrently. For example, the banking
system, railway, and air reservations systems, stock market
monitoring, supermarket inventory, and checkouts, etc.
• Not managing concurrent access may create issues like:
• Hardware failure and system crashes
• Concurrent execution of the same transaction, deadlock, or
slow performance
ACID Properties in DBMS

• DBMS is the management of data that should remain integrated when any
changes are done in it. It is because if the integrity of the data is affected,
whole data will get disturbed and corrupted. Therefore, to maintain the
integrity of the data, there are four properties described in the database
management system, which are known as the ACID properties. The ACID
properties are meant for the transaction that goes through a different
group of tasks, and there we come to see the role of the ACID properties.

• In this section, we will learn and understand about the ACID properties.
We will learn what these properties stand for and what does each
property is used for. We will also understand the ACID properties with the
help of some examples.
• 1) Atomicity: The term atomicity defines that the data
remains atomic. It means if any operation is performed on the
data, either it should be performed or executed completely or
should not be executed at all. It further means that the
operation should not break in between or execute partially. In
the case of executing operations on the transaction, the
operation should be completely executed and not partially.
• 4) Durability: Durability ensures the permanency of something. In DBMS,
the term durability ensures that the data after the successful execution of
the operation becomes permanent in the database. The durability of the
data should be so perfect that even if the system fails or leads to a crash,
the database still survives. However, if gets lost, it becomes the
responsibility of the recovery manager for ensuring the durability of the
database. For committing the values, the COMMIT command must be
used every time we make changes.
• Therefore, the ACID property of DBMS plays a vital role in maintaining the
consistency and availability of data in the database.
• Thus, it was a precise introduction of ACID properties in DBMS. We have
discussed these properties in the transaction section also.
Database Backup and Recovery

It is imperative to have a backup of the database in case the


original is corrupted or lost because of any reason. Using this
backup, the database can be recovered as it was before the
failure.
• Database backup basically means that a duplicate of the
database information and data is created and stored in
backup server just to be on the safe side. Transaction logs are
also stored in the backup along with the database data
because without them, the data would be useless.
Reasons of Failure in a Database

• There can be multiple reasons of failure in a database because of


which a database backup and recovery plan is required. Some of
these reasons are:
• User Error - Normally, user error is the biggest reason of data
destruction or corruption in a database. To rectify the error, the
database needs to be restored to the point in time before the error
occured.
• Hardware Failure - This can also lead to loss of data in a database.
The database is stored on multiple hard drives across various
locations. These hard drives may sometimes malfunction leading to
database corruption. So, it is important to periodically change
them.
• Catastrophic Event - A catastrophic event can be a natural calamity
like a flood or earthquake or deliberate sabotage such as hacking of
the database. Either way, the database data may be corrupted and
backup may be required.
Methods of Backup

• The different methods of backup in a database are:


• Full Backup - This method takes a lot of time as the full copy of the
database is made including the data and the transaction records.
• Transaction Log - Only the transaction logs are saved as the backup
in this method. To keep the backup file as small as possible, the
previous transaction log details are deleted once a new backup
record is made.
• Differential Backup - This is similar to full backup in that it stores
both the data and the transaction records. However only that
information is saved in the backup that has changed since the last
full backup. Because of this, differential backup leads to smaller
files.
Database Recovery

• There are two methods that are primarily used for database recovery.
These are:
• Log based recovery - In log based recovery, logs of all database
transactions are stored in a secure area so that in case of a system failure,
the database can recover the data. All log information, such as the time of
the transaction, its data etc. should be stored before the transaction is
executed.
• Shadow paging - In shadow paging, after the transaction is completed its
data is automatically stored for safekeeping. So, if the system crashes in
the middle of a transaction, changes made by it will not be reflected in the
database.
Difference between backup and recovery.
Backup
• It refers to storing a copy of the original data in a separate memory location.
• It is basically a copy of data that is used to restore the original data in case of a data
loss/damage.
• It is the replication of data.
• It just keeps one extra copy to ensure data is not lost forever.
• It helps in giving data protection.
• It makes the process of data recovery hassle free and easy.
• It is an affordable process.
• It is used in production environments.
• Example - SnapManager helps create a backup of data and everything else in the database.

Recovery
• It refers to restoring the lost data when there is failure.
• It is a process of retrieving lost or corrupted or damaged data to its original form.
• It is the process of storing the data back to the database.
• Backup is used to recover the data.
• It improves the reliability of the database.
• Recovery is an expensive process.
• It is not used frequently in production environments.
• The cost of recovery is expensive.
• Example: SnapManager helps recover the data to its last transaction

You might also like