Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Adbms Unit 3 Notes

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

Distributed Transaction Management

Basic Concepts of Transaction


Management
 Transaction management in a database system deals with the problems

of maintaining consistency of data in spite of system failures and

concurrent accesses to data.

 A transaction consists of a set of operations that perform a single

logical unit of work in a database environment.

 It may be an entire program, or a part of a program, or a single

command (SQL command), and it may involve any number of

operations on the database.

 If a transaction is completed successfully, then the database moves from

one consistent state to another.

 This consistency must be ensured irrespective of whether transactions

are successfully executed simultaneously or there are failures during

execution.

 Thus, a transaction is a unit of consistency and reliability. 


 Reliability refers to both the resilience of a system to various types of

failures and its capability to recover from them. A resilient system

continues its normal execution even when failures occur.

 A recoverable DBMS always keeps the database in a consistent state

either by moving back to a previous consistent state or by moving

forward to a new consistent state following various types of failures.

 The basic difference between a query and a transaction is the

fundamental properties of a transaction: atomicity and durability.

 A transaction ensures the consistency of the database irrespective of the

facts that several transactions are executed concurrently and that

failures may occur during their execution.

 A transaction can be considered to be made up of a sequence of read

and write operations on the database, together with some

computational steps.

 In that sense, a transaction may be thought of as a program with

embedded database access queries.

 Each transaction has to terminate.

 The outcome of the termination depends on the success or failure of the

transaction.
 Once a transaction starts execution, it may terminate in one of the

following possible ways:

 The transaction aborts if a failure occurred during its execution.

 The transaction commits if it was completed successfully.

 A transaction may abort due to a variety of reasons.

 When a transaction is aborted, to maintain the consistency of the data

its execution is stopped, and the already executed actions are undone by

returning the database to the prior state that existed before the start of

the execution. This is known as rollback. 

 If a transaction is committed, the result will permanently be stored in

the database, and this cannot be undone.

Example 7.1

Let us consider the following Employee relation:

 Employee = (emp-id, emp-name, designation, salary, emp-branch,


project-no)

and the SQL query

 “Update employee salary by 20% for the employee whose emp-id is


E001 in the Employee relation”.

The SQL statement for this query is:


      UPDATE EMPLOYEE           SET SALARY = SALARY * 1.2      WHERE EMP-
ID = “E001”.

This query can be represented as a transaction with the


name salary_update by using the embedded SQL notation as listed below.

      Begin-transaction salary_update      begin      EXEC SQL              UPDATE 
EMPLOYEE SET SALARY = SALARY * 1.2              WHERE EMP-ID = “E001”     
end

The Begin-transaction and end statements represent the beginning and end


of a transaction respectively, which is treated as a program that performs a
database access.

ACID Properties of Transactions


 A transaction has four properties that lead to the consistency and
reliability of a database management system.

These are

1. Atomicity
2. Consistency
3. Isolation
4. Durability.

Atomicity :
 Atomicity refers to the fact that each transaction is a single logical
unit of work in a database environment that may consist of a
number of operations.
 Thus, either all operations of a transaction are to be completed
successfully, or none of them are carried out at all.
 This property is known as all-or-nothing property of a transaction.
 It is the responsibility of the DBMS to maintain atomicity even
when failures occur in the system.
 If the execution of a transaction is interrupted by any sort of
failures, the DBMS will determine what actions should be
performed with that transaction to recover from the failed state.
 To maintain atomicity during failures, either the system should
complete the remaining operations of the transaction or it should
undo all the operations that have already been executed by the
transaction before the failure.
 The execution of a transaction may be interrupted owing to a
variety of reasons such as bad input data, deadlock, system crash,
processor failures, power failure, or media and communication
links failures (in case of distributed system).
 The activity of ensuring transaction atomicity in the case of
transaction aborts due to failures caused by bad input data,
deadlock and other factors (except system crash and hardware
failures) is called transaction recovery.
 On the other hand, the activity of preserving transaction atomicity
in the case of system crash or hardware failures is called crash
recovery.

Consistency :

 It Reffers to the correctness of data, this property states that a


transaction must transform the database from one consistent
state to another consistent state.
 It is the responsibility of both the semantic data controller
(integrity manager) and the concurrency control mechanisms
to maintain consistency of the data.
 The semantic data controller can ensure consistency by
enforcing all the constraints that have been specified in the
database schema, such as integrity and enterprise constraints.
 Similarly, the responsibility of a concurrency control
mechanism is to disallow transactions from reading or
updating “dirty data”.
 Dirty data refers to the state of the data at which the data have
been updated by a transaction, but the transaction has not yet
committed.
 A classification of consistency has been defined based on dirty
data, which groups databases into four levels of consistency,
which are listed in the following.

Degree 3: A transaction T1 observes degree 3 consistency if it is


restricted by the following rules:

1. T1 does not overwrite dirty data of other transactions.


2. T1 does not commit any write operation until it completes all its
write operations, that is, until the end of the transaction.
3. T1 does not read dirty data from other transactions.
4. Other transactions in the system do not read any dirty data
updated by T1 before T1 completes.

Degree 2: A transaction T1 observes degree 2 consistency if it is


restricted by the following rules.

1. T1 does not overwrite dirty data of other transactions.


2. T1 does not commit any write operation until it completes all its
write operations, that is, until the end of the transaction.
3. T1 does not read dirty data from other transactions.

Degree 1: A transaction T1 observes degree 1 consistency if it is


restricted by the following rules.

1. T1 does not overwrite dirty data of other transactions.


2. T1 does not commit any write operation until it completes all its
write operations, that is, until the end of the transaction.

Degree 0: A transaction T1 observes degree 0 consistency if it is


restricted by the following facts.

1. T1 does not overwrite dirty data of other transactions.


Thus, it is clear that a higher degree of consistency covers all the lower
levels. These different degrees of consistency provide flexibility to
application developers to define transactions that can operate at
different levels.

Isolation:

 According to this property, transactions can execute


independently in a database environment; that is, they are not
dependent on each other.
 In other words, in a database environment, transactions are
isolated from one another and the partial effect of incomplete
transactions should not be visible to other transactions.
 This isolation property of transactions is ensured by the
concurrency control mechanism of a DBMS.
 Consequently, no transaction can read or modify data that is
being modified by another transaction.
 If this property is not maintained, one of the following two
events as listed below can take place in the database.

1. Lost update –. This problem occurs when an apparently


successful completed update operation made by one
transaction (first) is overridden by another transaction
(second). The lost update problem is illustrated in example 7.2.

Example 7.2. 
Assume that there are two concurrent transactions T1 and T2
respectively occurring in a database environment. The
transaction T1 is withdrawing $500 from an account with
balance B, with an initial value of $2,000, while the
transaction T2 is depositing an amount of $600 into the same
account. If these two transactions are executed serially, then
the balance B of the account will be $2,100. Consider that both
the transactions are started nearly at the same time, and both
have read the same value of B, that is, $2,000. Owing to the
concurrent access to the same data item, when the
transaction T1 is decrementing the value of B to $1,500, the
transaction T2 is incrementing the value of B to $2,600. Thus,
update to the data item B made by the transaction T1 will be
overridden by the transaction T2. This is known as lost update
problem.

2. Cascading aborts –. This problem occurs when one


transaction allows other transactions to read its uncommitted
data and decides to abort; the transactions that have read or
modified its uncommitted data will also abort. Consider the
previous example 7.2. If transaction T1 permits transaction T2
to read its uncommitted data, then if transaction T1 aborts,
transaction T2 will also abort consequently.
The type of isolation that is ensured by not allowing
uncommitted data to be seen by other transactions is
called cursor stability. It is obvious that higher levels of
consistency provide more isolation among transactions. If
different degrees of consistency are considered from the point
of view of the isolation property, then degree 0 prevents lost
update problem, degree 2 prevents cascading aborts and
degree 3 provides the full isolation by restricting both the
above problems.

A set of isolation levels has been defined by ANSI SQL2 [ANSI, 1992]
based on the events dirty read, non-repeatable or fuzzy read and
phantom read, when full isolation is not maintained in a database
environment. These are read uncommitted, read committed,
repeatable read and anomaly serializable. Dirty read refers to
the situation when a transaction reads the dirty data of another
transaction. Fuzzy read or non-repeatable read occurs when one
transaction reads the value of a data item that is modified or deleted
by another transaction. If the first transaction attempts to re-read
the data item, either that data item may not be found or it may read
a different value. Similarly, it is possible that when a transaction is
performing a search operation depending on a selection predicate,
another transaction may be inserting new tuples into the database
satisfying that predicate. This is known as phantom read. The
different isolation levels based on these events are listed below.

 Read uncommitted –. At this isolation level, all the three


above events may occur.
 Read committed –. At this isolation level, non-repeatable read
and phantom read are possible, but dirty read cannot occur.
 Repeatable read –. Only phantom read is possible at this
isolation level.
 Anomaly serializable –. At this isolation level, none of the
above events – dirty read, phantom read and fuzzy read – is
possible.

Example 7.3. 

Assume that there are two concurrent transactions T1 and T2


respectively occurring in a database environment. The
transaction T1 is depositing $500 to an account X with an initial
value $3,000, while the transaction T2 is withdrawing an amount
$1,000 from the same account X. Further assume that the
transaction T1 is executed first and allows the transaction T2 to
read the uncommitted value $3,500 for X and then aborts. This
problem is known as dirty read problem, which is illustrated in the
following:
 T1: write(X)
 T2: read(X)
 T1: abort

Thus, the transaction T2 reads the dirty data of the transaction T1.

Example 7.4. 

In a DBMS, consider a situation when one transaction T1 reads the


value of a data item X, while another transaction T2 updates the
same data item. Now, if the transaction T1 reads the same data
item X again, it will read some different value, because the data item
is already manipulated by the transaction T2. This problem is
known as non-repeatable or fuzzy read problem, which is illustrated
in the following:

 T1: read(X)
 T2: write(X)
 T2: end transaction
 T1: read(X)

In this case, transaction T1 has read two different values for X.

Example 7.5. 

The phantom read problem is illustrated below.


 T1: read range [X – Y]
 T2: insert Z, X < Z < Y
 T2: end transaction
 T1: read range [X – Y]

In this case, when transaction T1 performs an operation depending


on a criterion, transaction T2 inserts a new tuple Z satisfying that
criterion. Now, if the transaction T1 performs the same operation
again, the result will be different. Hence, Z is a “phantom” data item.

Durability:

 Durability refers to the fact that the effects of a successfully


completed (committed) transaction are permanently recorded
in the database and will not be affected by a subsequent failure.
 This property ensures that once a transaction commits, the
changes are durable and cannot be erased from the database.
 It is the responsibility of the recovery subsystem of a DBMS to
maintain durability property of transactions.
 The recovery manager determines how the database is to be
recovered to a consistent state (database recovery) in which all
the committed actions are reflected.

Objectives of Distributed Transaction Management


 The responsibilities of the transaction manager in a
distributed DBMS is same as those of a corresponding
subsystem in a centralized database system, but its task is
complicated due to fragmentation, replication and the overall
distributed nature of the database.
 The consistency of the database should not be violated because
of transaction executions.
 Therefore, the primary objective of a transaction manger is to
execute transactions in an efficient way in order to preserve
the ACID properties of transactions (local as well as global
transactions) irrespective of concurrent execution of
transactions and system failures.
 At the same time, the efficiency of transaction executions
should be improved to meet the performance criteria.

In addition, a good transaction management scheme must consider


the following issues.

1. CPU and main memory utilization should be improved: 

 Most of the typical database applications spend much of


their time waiting for I/O operations rather than on
computations.
 In a large system, the concurrent execution of these I/O
bound applications can turn into a bottleneck in main
memory or in CPU time utilization.
 To solve problem and to improve CPU and main memory
utilization, a transaction manager should adopt
specialized techniques to deal with specific database
applications.
 This aspect is common to both centralized and
distributed DBMSs.

2. Response time should be minimized:

 To improve the performance of transaction executions,


the response time of each individual transaction must be
considered and should be minimized.
 In the case of distributed applications, it is very difficult
to achieve an acceptable response time.

3. Availability should be maximized:

 Although the availability in a distributed system is better


than that in a centralized system, it must be maximized
for transaction recovery and concurrency control in
distributed databases.
4. Communication cost should be minimized:

 In a distributed system an additional communication cost


is incurred for each distributed or global application,
because a number of message transfers are required
between sites to control the execution of a global
application.
 These messages are not only used to transfer data, but are
required to control the execution of the application.
 Preventative measures should be adopted by the
transaction manager to minimize the communication
cost.

A Model for Transaction Management in a Distributed


System
 It is an abstract model for transaction management in a
distributed database environment.
 In a distributed system, transactions are classified into two
different categories:
1. Local transactions 
2. Global transactions (or distributed transactions).
 If the data requirement of a transaction can be fulfilled from
the local site, it is called a local transaction. Local transactions
access data only from local sites.
 On the other hand, global transactions access data from
remote sites or multiple sites.
 It is obvious that the transaction management in a distributed
DBMS is more complicated than in a centralized DBMS, as the
distributed DBMS must ensure the atomicity of the global
transaction as well as of each component sub-transaction
executed at the local sites.
 This complicated task is performed by four high-level
interconnected modules of the DBMS. These are
1. Transaction manager,
2. Concurrency control manager,
3. Recovery manager
4. Buffer manager.
 The transaction manager coordinates transactions on behalf
of application programs by communicating with the scheduler
and implements a particular strategy for concurrency control.
 The responsibility of the concurrency control manager is to
maximize concurrency, without allowing concurrently
executing transactions to interfere with one another, and
thereby maintain the consistency of the database as well as the
isolation property of the transactions.
 The recovery manager preserves the database in a consistent
state in case of failures.
 The buffer manager manages the efficient transfer of data
between disk storage and main memory.
 In a distributed DBMS, all these modules exist in each local
DBMS.
 In addition, a global transaction manager or transaction
coordinator is required at each site to control the execution of
global transactions as well as of local transactions initiated at
that site.
 Therefore, an abstract model of transaction management at
each site of a distributed system consists of two different sub-
modules:
1. The transaction manager and
2. The transaction coordinator.
3. Transaction manager:
 The transaction manager at each site manages the execution of
the transactions that access data stored at that local site.
 Each such transaction may be a local transaction or part of a
global transaction.
 The structure of the transaction manager is similar to that of
its counterpart in a centralized system, and it is responsible for
the following:
 Maintaining a log for recovery purposes in case of failures.
 Implementing an appropriate concurrency control mechanism
to coordinate the concurrent execution of transactions
executing at that local site.

Transaction coordinator: 
 The transaction coordinator at each site in a distributed
system coordinates the execution of both local and global
transactions initiated at that site.
 This component or module is not required in centralized
DBMSs, as there is only one site in a centralized system.
 The transaction coordinator at each site performs the
following tasks to coordinate the execution of transactions
initiated at that local site.
 It starts the execution of the transactions initiated at that site.
 It breaks the transactions into a number of sub-transactions
and distributes these sub-transactions to the appropriate sites
for execution.
 It coordinates the termination of the transactions, which may
result in the transactions being committed at all sites or
aborted at all sites.

The structure of this model is depicted in figure 7.1.


Figure 7.1. A Model for Transaction Management at Each Site in a DDBMS

 A transaction is considered as a part of an application.


 When a user requests for the execution of an application (may
be local or global), the application issues
a begin_transaction primitive.
 All actions that are performed by the application after that are
to be considered part of the same transaction until
a commit or an abort primitive is issued.
 To execute a global application generated at site S1, the
transaction coordinator of site S1 initiates the transaction and
breaks the transaction into a number of sub-transactions.
 It then involves multiple sites by consulting the global system
catalog for parallel execution of these sub-transactions at
different sites.
 When these sub-transactions are distributed over multiple
local sites and perform some tasks on behalf of the application,
they are called agents. 
 Agents reside at multiple sites and communicate with each
other via message passing.
 The transaction manager at a local site maintains the log when
a local transaction or part of a global transaction is executed at
that local site.
 It also controls the concurrent execution of transactions
executing at that site.
 The results retrieved from the parallel execution of these sub-
transactions at multiple sites are integrated by the transaction
coordinator of site S1, and finally the transaction is terminated.

Classification of Transactions
 These classifications have been defined based on various
criteria such as the lifetime of the transaction, organization of
read and write operations within the transaction and the
structure of the transaction.
 Depending on their lifetime or duration, transactions can be
classified into two broad categories: 
1. Short-duration transactions 
2. Long-duration transactions. 
 A short-duration transaction (also called online
transaction) requires a very short execution/response time
and accesses a relatively small portion of the database.
 On the other hand, a long-duration transaction (also called
a batch transaction) requires a longer execution/response
time and generally accesses a larger portion of the database.
 Most of the statistical applications – report generation,
complex queries, image processing – are characterized by long-
duration transactions,
 whereas most of the current normal database applications
including railway reservation, banking system, project
management require the short-duration category transactions.
 Another way to divide transactions into different classes is
based on the organization of the read and the write operations
within the transaction.
 In some transactions, read and write operations are organized
in a mixed manner; they do not appear in any specific order.
These are called general transactions. 
 Some transactions restrict the organization of read and write
operations in such a way that all the read operations within the
transaction are performed before the execution of any write
operation. These transactions are called two-step
transactions.
 Similarly, some transactions may restrict that a data item has
to be read before it can be written (updated) within the
transaction operation. These transactions are known
as restricted (or read-before-write) transactions.
 If a transaction is characterized by features of both two-step
and restricted transactions, then it is called a restricted two-
step transaction.
 Transactions can also be classified into three broad categories
based on the structure of transactions, namely,
1. Flat transactions
2. Nested transactions 
3. Workflow models.
 A flat transaction has a single start point (begin_transaction)
and a single end point or termination point (end_transaction).
 Most of the typical transaction management database
applications are characterized by flat transactions.
 Flat transactions are relatively simple and perform well in
short activities, but they are not well-suited for longer
activities.
 A nested transaction includes other transactions within its
own start point and end point; thus, several transactions,
called subtransactions, may be nested into one transaction.
o A nested transaction is further classified into two
categories: closed nested transaction and open nested
transaction.
o In a closed nested transaction commit occurs in a
bottom-up fashion, that is, parent transactions are
committed after the commit of sub-transactions or child
transactions that are nested within it, although sub-
transactions are started after the parent transaction.
o On the other hand, open nested transactions provide
flexibility by allowing the commit of the parent
transaction before the commit of sub-transactions.

o The primary advantage of a nested transaction is that it


provides a higher level of concurrency among
transactions, as a number of sub-transactions may be
nested or embedded into one transaction.
o The nested transaction is also beneficial for recovery, as it
is possible to recover independently each sub-transaction
from failures, thus, leading to less expensive recovery
control mechanisms.
o In the case of nested transactions it is possible to restart
only the operations of a particular sub-transaction
instead of restarting the entire transaction when failures
occur in the system.
o Finally, the creation of a new transaction is not
cumbersome as this can be done by simply inserting the
newly created transaction into an existing transaction as
a sub-transaction.
 The name of the third category of transactions based on
transaction structure is workflow.
o A workflow is defined as a collection of tasks organized
to carry out some business process.
o Three different types of workflow which are listed in the
following:
o Human-oriented workflows –. These workflows
provide support for collaboration and coordination
among humans, but humans themselves are ultimately
responsible for the consistency of the operations.
o System-oriented workflows –. These workflows provide
support for specialized tasks that can be executed by a
computer such as concurrency control and recovery,
automatic task execution, and notification.
o Transactional workflows –. This type of workflows has
the characteristics of both human-oriented and system-
oriented workflows.
 These workflows provide supports for coordinating
execution of multiple tasks and selective use of
transactional properties for individual tasks or
entire workflows.
o There are two general issues involved in workflows:
specification of workflows and execution of workflows.
The key issues involved in specifying a workflow are as
follows:
o Task specification –. The execution structure of each
task is defined by providing a set of externally observable
execution states and a set of transitions between these
states.
o Task coordination requirements –. These are usually
represented as inter-task execution dependencies and
data flow dependencies, and the termination conditions
of the workflow.
o Execution requirements –. These restrict the execution
of the workflow to fulfill application-specific correctness
criteria.
 These include failure/execution atomicity
requirements, workflow concurrency control, and
recovery requirements.
o Example 
 Let us consider the example of patient’s health checkup
transaction. The entire health checkup activity consists of the
following tasks and involves the following data.
 Task1 (T1): A patient requests for health checkup registration
and Patient database is accessed.
 Task2 (T2 & T3): Some medical tests have been performed,
and the patient’s health information is stored in the Health
database. In this case, several medical tests can be performed
simultaneously. For simplicity, here it is assumed that two
medical tests have been performed.
 Task3 (T4): The doctor’s advice is taken and hence Doctor
database is accessed.
 Task4 (T5): A bill is generated and the billing information is
recorded into the database.
o In this patient’s health checkup transaction, there is a
serial dependency of T2, T3 on T1, and T4 on T2, T3.
Hence, T2 and T3 can be performed in parallel and T5
waits until their completion. The corresponding workflow
is illustrated in below figure.
Figure 7.2. An Example of a Workflow

You might also like