Unit 4
Unit 4
If some kind of error occurs during this state, the transaction goes into a failed
state, else it goes into the Committed state.
iii) Commited
This state of transaction is achieved when all the transaction-related operations have
been executed successfully along with the Commit operation, i.e. data is saved into
the database after the required manipulations in this state. This marks the successful
completion of a transaction.
iv) Failed
If any of the transaction-related operations cause an error during the active or
partially committed state, further execution of the transaction is stopped and it is
brought into a failed state. Here, the database recovery system makes sure that the
database is in a consistent state.
v) Aborted
If the error is not resolved in the failed state, then the transaction is aborted and a
rollback operation is performed to bring database to the the last saved consistent state.
When the transaction is aborted, the database recovery module either restarts the
transaction or kills it.
The illustration below shows the various states that a transaction may encounter in its
completion journey.
Transaction in DBMS
Properties of Transaction
As transactions deal with accessing and modifying the contents of the database,
they must have some basic properties which help maintain the consistency and
integrity of the database before and after the transaction. Transactions follow 4
properties, namely, Atomicity, Consistency, Isolation, and Durability.
Generally, these are referred to as ACID properties of transactions in DBMS.
ACID is the acronym used for transaction properties. A brief description of each
property of the transaction is as follows.
i) Atomicity
This property ensures that either all operations of a transaction are executed or it is
aborted. In any case, a transaction can never be completed partially.
Each transaction is treated as a single unit (like an atom). Atomicity is achieved
through commit and rollback operations, i.e. changes are made to the database
only if all operations related to a transaction are completed, and if it gets
interrupted, any changes made are rolled back using rollback operation to bring the
database to its last saved state.
ii) Consistency
This property of a transaction keeps the database consistent before and after a
transaction is completed.
Execution of any transaction must ensure that after its execution, the database is
either in its prior stable state or a new stable state.
In other words, the result of a transaction should be the transformation of a
database from one consistent state to another consistent state.
Consistency, here means, that the changes made in the database are a result of
logical operations only which the user desired to perform and there is not any
ambiguity.
iii) Isolation
This property states that two transactions must not interfere with each other, i.e. if
some data is used by a transaction for its execution, then any other transaction can
not concurrently access that data until the first transaction has completed.
It ensures that the integrity of the database is maintained and we don’t get any
ambiguous values. Thus, any two transactions are isolated from each other.
This property is enforced by the concurrency control subsystem of DBMS.
iv) Durability
This property ensures that the changes made to the database after a transaction is
completely executed, are durable.
It indicates that permanent changes are made by the successful execution of a
transaction.
In the event of any system failures or crashes, the consistent state achieved after
the completion of a transaction remains intact. The recovery subsystem of DBMS
is responsible for enforcing this property.
Conclusion
Transactions in DBMS are pivotal in maintaining the integrity and consistency of the
database through a series of well-defined operations and states. From the initial
execution of operations to handling errors and ensuring consistency, transactions must
adhere to the ACID properties—Atomicity, Consistency, Isolation, and Durability.
These properties guarantee that transactions are processed reliably and that the
database remains stable even in the face of failures or concurrent operations.
Serializability in DBMS
In this article, we are going to explain the serializability concept and how this concept
affects the DBMS deeply, we also understand the concept of serializability with some
examples, and we will finally conclude this topic with an example of the importance
of serializability. The DBMS form is the foundation of the most modern applications,
and when we design the form properly, it provides high-performance and relative
storage solutions to our application.
What is a serializable schedule, and what is it used for?
If a non-serial schedule can be transformed into its corresponding serial schedule, it is
said to be serializable. Simply said, a non-serial schedule is referred to as a
serializable schedule if it yields the same results as a serial timetable.
Non-serial Schedule
A schedule where the transactions are overlapping or switching places. As they are
used to carry out actual database operations, multiple transactions are running at once.
It’s possible that these transactions are focusing on the same data set. Therefore, it is
crucial that non-serial schedules can be serialized in order for our database to be
consistent both before and after the transactions are executed.
Example:
Transaction-1 Transaction-2
R(a)
W(a)
R(b)
W(b)
R(b)
R(a)
W(b)
W(a)
Precedence Graph
It can be described as a Graph G(V, E) with vertices V = “V1, V2, V3,…, Vn” and
directed edges E = “E1, E2, E3,…, En”. One of the two operations—READ or
WRITE—performed by a certain transaction is contained in the collection of edges.
Where Ti -> Tj, means Transaction-Ti is either performing read or write before the
transaction-Tj.
Types of Serializability
There are two ways to check whether any non-serial schedule is serializable.
1. Conflict serializability
Conflict serializability refers to a subset of serializability that focuses on maintaining
the consistency of a database while ensuring that identical data items are executed in
an order. In a DBMS each transaction has a value and all the transactions, in the
database rely on this uniqueness. This uniqueness ensures that no two operations with
the conflict value can occur simultaneously.
For example lets consider an order table and a customer table as two instances. Each
order is associated with one customer even though a single client may place orders.
However there are restrictions for achieving conflict serializability in the database.
Here are a few of them.
1. Different transactions should be used for the two procedures.
2. The identical data item should be present in both transactions.
3. Between the two operations, there should be at least one write operation.
Example
Three transactions—t1, t2, and t3—are active on a schedule “S” at once. Let’s create a
graph of precedence.
Transaction – 1 (t1) Transaction – 2 (t2) Transaction – 3 (t3)
R(a)
R(b)
R(b)
Transaction – 1 (t1) Transaction – 2 (t2) Transaction – 3 (t3)
W(b)
W(a)
W(a)
R(a)
W(a)
DAG of transactions
R(a)
Transaction-1 (t1) Transaction-2 (t2)
W(a)
R(a)
W(a)
R(b)
W(b)
R(b)
W(b)
R(a)
W(a)
R(b)
W(b)
R(a)
W(a)
R(b)
W(b)
It is a view serializable schedule since a view similar schedule is conceivable.
Note: A conflict serializable schedule is always viewed as
serializable, but vice versa is not always true.
Advantages of Serializability
1. Execution is predictable: In serializable, the DBMS’s threads are all performed
simultaneously. The DBMS doesn’t include any such surprises. In DBMS, no data
loss or corruption occurs and all variables are updated as intended.
2. DBMS executes each thread independently, making it much simpler to understand
and troubleshoot each database thread. This can greatly simplify the debugging
process. The concurrent process is therefore not a concern for us.
3. Lower Costs: The cost of the hardware required for the efficient operation of the
database can be decreased with the aid of the serializable property. It may also
lower the price of developing the software.
4. Increased Performance: Since serializable executions provide developers the
opportunity to optimize their code for performance, they occasionally outperform
non-serializable equivalents.
For a DBMS transaction to be regarded as serializable, it must adhere to the ACID
properties. In DBMS, serializability comes in a variety of forms, each having
advantages and disadvantages of its own. Most of the time, choosing the best sort of
serializability involves making a choice between performance and correctness.
Making the incorrect choice for serializability might result in database issues that are
challenging to track down and resolve. You should now have a better knowledge of
how serializability in DBMS functions and the different types that are available thanks
to this guide.
Recoverability in DBMS
Recoverability is a property of database systems that ensures that, in the event of a
failure or error, the system can recover the database to a consistent state.
Recoverability guarantees that all committed transactions are durable and that their
effects are permanently stored in the database, while the effects of uncommitted
transactions are undone to maintain data consistency.
The recoverability property is enforced through the use of transaction logs, which
record all changes made to the database during transaction processing. When a failure
occurs, the system uses the log to recover the database to a consistent state, which
involves either undoing the effects of uncommitted transactions or redoing the effects
of committed transactions.
There are several levels of recoverability that can be supported by a database system:
No-undo logging: This level of recoverability only guarantees that committed
transactions are durable, but does not provide the ability to undo the effects of
uncommitted transactions.
Undo logging: This level of recoverability provides the ability to undo the effects of
uncommitted transactions but may result in the loss of updates made by committed
transactions that occur after the failed transaction.
Redo logging: This level of recoverability provides the ability to redo the effects of
committed transactions, ensuring that all committed updates are durable and can be
recovered in the event of failure.
Undo-redo logging: This level of recoverability provides both undo and redo
capabilities, ensuring that the system can recover to a consistent state regardless of
whether a transaction has been committed or not.
In addition to these levels of recoverability, database systems may also use techniques
such as checkpointing and shadow paging to improve recovery performance and
reduce the overhead associated with logging.
Overall, recoverability is a crucial property of database systems, as it ensures that data
is consistent and durable even in the event of failures or errors. It is important for
database administrators to understand the level of recoverability provided by their
system and to configure it appropriately to meet their application’s requirements.
Prerequisite:
Introduction to Concurrency Control
Types of Schedules
As discussed, a transaction may not execute completely due to hardware failure,
system crash or software issues. In that case, we have to roll back the failed
transaction. But some other transactions may also have used values produced by the
failed transaction. So we have to roll back those transactions as well.
Recoverable Schedules:
Schedules in which transactions commit only after all transactions whose changes
they read commit are called recoverable schedules. In other words, 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.
Example 1:
S1: R1(x), W1(x), R2(x), R1(y), R2(y),
W2(x), W1(y), C1, C2;
Given schedule follows order of Ti->Tj => C1->C2. Transaction T1 is executed
before T2 hence there is no chances of conflict occur. R1(x) appears before W1(x)
and transaction T1 is committed before T2 i.e. completion of first transaction
performed first update on data item x, hence given schedule is recoverable.
Example 2: Consider the following schedule involving two transactions T 1 and T2.
T1 T2
R(A)
W(A)
W(A)
R(A)
commit
commit
This is a recoverable schedule since T 1 commits before T2, that makes the value read
by T2 correct.
Irrecoverable Schedule: The table below shows a schedule with two transactions, T1
reads and writes A and that value is read and written by T2. T2 commits. But later on,
T1 fails. So we have to rollback T1. Since T2 has read the value written by T1, it
should also be rollbacked. But we have already committed that. So this schedule is
irrecoverable schedule. When Tj is reading the value updated by Ti and Tj is
committed before committing of Ti, the schedule will be irrecoverable.
Recoverable with Cascading Rollback: The table below shows a schedule with two
transactions, T1 reads and writes A and that value is read and written by T2. But later
on, T1 fails. So we have to rollback T1. Since T2 has read the value written by T1, it
should also be rollbacked. As it has not committed, we can rollback T2 as well. So it
is recoverable with cascading rollback. Therefore, if Tj is reading value updated by Ti
and commit of Tj is delayed till commit of Ti, the schedule is called recoverable with
cascading rollback.
Cascadeless Recoverable Rollback: The table below shows a schedule with two
transactions, T1 reads and writes A and commits and that value is read by T2. But if
T1 fails before commit, no other transaction has read its value, so there is no need to
rollback other transaction. So this is a Cascadeless recoverable schedule. So, if Tj
reads value updated by Ti only after Ti is committed, the schedule will be cascadeless
recoverable.
Atomicity: Transactions in a DBMS are designed to be atomic, which means that they
either entire absolutely or are rolled back to their unique nation in case of a failure.
This guarantees that the database is usually in a consistent nation.
Durability: Once a transaction is dedicated, its changes are permanently stored to the
database, even in the occasion of a failure. This ensures that the database may be
restored to its closing consistent kingdom after a failure.
Logging: A DBMS keeps a log of all transactions to make sure recoverability. The
log consists of data about all adjustments made to the database, as well as the
transactions that made those changes. In the event of a failure, the log may be used to
repair the database to a regular state.
Checkpointing: A checkpoint is a point in time in which the DBMS records the
country of the database and logs it. This allows lessen the quantity of time required for
recovery in case of a failure, as handiest the transactions since the last checkpoint
need to be rolled back or replayed.
Recovery manager: A restoration supervisor is a part of a DBMS that is accountable
for restoring the database to a constant state after a failure. The healing supervisor
uses the log and checkpoints to determine which transactions want to be rolled
returned or replayed to repair the database.
Media recuperation: Media recovery refers back to the capability of a DBMS to
recover from a failure that affects the garage media, such as a hard disk crash. This
includes restoring the database from a backup and making use of the log to deliver it
up to date.
Deadlock in DBMS
In database management systems (DBMS) a deadlock occurs when two or more
transactions are unable to the proceed because each transaction is waiting for the other
to the release locks on resources. This situation creates a cycle of the dependencies
where no transaction can continue leading to the standstill in the system. The
Deadlocks can severely impact the performance and reliability of a DBMS making it
crucial to the understand and manage them effectively.
What is Deadlock?
The Deadlock is a condition in a multi-user database environment where transactions
are unable to the complete because they are each waiting for the resources held by
other transactions. This results in a cycle of the dependencies where no transaction
can proceed.
Characteristics of Deadlock
Mutual Exclusion: Only one transaction can hold a particular resource at a time.
Hold and Wait: The Transactions holding resources may request additional
resources held by others.
No Preemption: The Resources cannot be forcibly taken from the transaction
holding them.
Circular Wait: A cycle of transactions exists where each transaction is waiting for
the resource held by the next transaction in the cycle.
In a database management system (DBMS), a deadlock occurs when two or more
transactions are waiting for each other to release resources, such as locks on database
objects, that they need to complete their operations. As a result, none of the
transactions can proceed, leading to a situation where they are stuck or “deadlocked.”
Deadlocks can happen in multi-user environments when two or more transactions are
running concurrently and try to access the same data in a different order. When this
happens, one transaction may hold a lock on a resource that another transaction needs,
while the second transaction may hold a lock on a resource that the first transaction
needs. Both transactions are then blocked, waiting for the other to release the resource
they need.
DBMSs often use various techniques to detect and resolve deadlocks automatically.
These techniques include timeout mechanisms, where a transaction is forced to release
its locks after a certain period of time, and deadlock detection algorithms, which
periodically scan the transaction log for deadlock cycles and then choose a transaction
to abort to resolve the deadlock.
It is also possible to prevent deadlocks by careful design of transactions, such as
always acquiring locks in the same order or releasing locks as soon as possible. Proper
design of the database schema and application can also help to minimize the
likelihood of deadlocks.
In a database, a deadlock is an unwanted situation in which two or more transactions
are waiting indefinitely for one another to give up locks. Deadlock is said to be one of
the most feared complications in DBMS as it brings the whole system to a Halt.
Example – let us understand the concept of deadlock suppose, Transaction T1 holds a
lock on some rows in the Students table and needs to update some rows in the Grades
table. Simultaneously, Transaction T2 holds locks on those very rows (Which T1
needs to update) in the Grades table but needs to update the rows in the Student
table held by Transaction T1.
Now, the main problem arises. Transaction T1 will wait for transaction T2 to give up
the lock, and similarly, transaction T2 will wait for transaction T1 to give up the lock.
As a consequence, All activity comes to a halt and remains at a standstill forever
unless the DBMS detects the deadlock and aborts one of the transactions.
Deadlock in DBMS
In this, older transactions must wait for the In this, older transactions never wait for
younger one to release its data items. younger transactions.
The number of aborts and rollbacks is higher in In this, the number of aborts and
these techniques. rollback is lesser.
Applications
1. Delayed Transactions: Deadlocks can cause transactions to be delayed, as the
resources they need are being held by other transactions. This can lead to slower
response times and longer wait times for users.
2. Lost Transactions: In some cases, deadlocks can cause transactions to be lost or
aborted, which can result in data inconsistencies or other issues.
3. Reduced Concurrency: Deadlocks can reduce the level of concurrency in the
system, as transactions are blocked waiting for resources to become available. This
can lead to slower transaction processing and reduced overall throughput.
4. Increased Resource Usage: Deadlocks can result in increased resource usage, as
transactions that are blocked waiting for resources to become available continue to
consume system resources. This can lead to performance degradation and
increased resource contention.
5. Reduced User Satisfaction: Deadlocks can lead to a perception of poor system
performance and can reduce user satisfaction with the application. This can have a
negative impact on user adoption and retention.
Features of Deadlock in a DBMS
1. Mutual Exclusion: Each resource can be held by only one transaction at a time,
and other transactions must wait for it to be released.
2. Hold and Wait: Transactions can request resources while holding on to resources
already allocated to them.
3. No Preemption: Resources cannot be taken away from a transaction forcibly, and
the transaction must release them voluntarily.
4. Circular Wait: Transactions are waiting for resources in a circular chain, where
each transaction is waiting for a resource held by the next transaction in the chain.
5. Indefinite Blocking: Transactions are blocked indefinitely, waiting for resources
to become available, and no transaction can proceed.
6. System Stagnation: Deadlock leads to system stagnation, where no transaction
can proceed, and the system is unable to make any progress.
7. Inconsistent Data: Deadlock can lead to inconsistent data if transactions are
unable to complete and leave the database in an intermediate state.
8. Difficult to Detect and Resolve: Deadlock can be difficult to detect and resolve,
as it may involve multiple transactions, resources, and dependencies.
Disadvantages
1. System downtime: Deadlock can cause system downtime, which can result in loss
of productivity and revenue for businesses that rely on the DBMS.
2. Resource waste: When transactions are waiting for resources, these resources are
not being used, leading to wasted resources and decreased system efficiency.
3. Reduced concurrency: Deadlock can lead to a decrease in system concurrency,
which can result in slower transaction processing and reduced throughput.
4. Complex resolution: Resolving deadlock can be a complex and time-consuming
process, requiring system administrators to intervene and manually resolve the
deadlock.
5. Increased system overhead: The mechanisms used to detect and resolve
deadlock, such as timeouts and rollbacks, can increase system overhead, leading to
decreased performance.