DBMS Module 4
DBMS Module 4
CHAPTER-11
TRANSACTION
PROCESSING
Transaction processing:
The concept of transaction provides a mechanism for describing logicalunits of
database processing.
Transaction processing systems are systems with large databases andhundreds
of concurrent users executing database transactions.
Examples of such systems include airline reservations, banking, credit cardprocessing,
online retail purchasing, stock markets, supermarket checkouts, and many other
applications.
These systems require high availability and fast response time forhundreds
of concurrent users.
1. Single user
2. Multiuser.
Single - User:
Multi-User:
o Many users can use the system and access the database concurrently.
o Database systems used in banks, insurance agencies, stock exchanges, supermarkets,
airline agencies and many other applications are multiusersystems.
o Multiple users can access databases and use computer system simultaneously
because of the concept of multiprogramming (to executemultiple programs or
processes at the same time).
o In a multiuser DBMS, the stored data items are the primary resources that may be
accessed concurrently by users or programs, which are constantly retrieving
information from and modifying the database.
Interleaved processing versus parallel processing of concurrent
transactions:
o Multiprogramming operating systems execute some commands from one process,
then suspend that process and execute some commands from thenext process, and so
on. A process is resumed at the point where it was suspended whenever it gets its turn
to use the CPU again. Hence, concurrent execution of processes is actually interleaved.
Interleaving prevents a long process from delaying other processes.
For example,
write transaction.
Database items:
3. Copy item X from the program variable named X into its correctlocation in
the buffer.
4. Store the updated disk block from the buffer back to disk (either
immediately or at some later point in time).
Sometimes the buffer is not immediately stored to disk, in case additionalchanges are
to be made to the buffer.
The decision about when to store a modified disk block whose contentsare in a main
memory buffer is handled by the recovery manager of theDBMS in cooperation with
the underlying operating system.
The DBMS will maintain in the database cache a number of data buffers inmain
memory.
Each buffer typically holds the contents of one database disk block, which contains
some of the database items being processed. When these buffersare all occupied, and
additional database disk blocks must be copied into memory, some buffer replacement
policy is used that is LRU(least recentlyused).If the chosen buffer has been modified, it
must be written back to disk before it is reused.
For example,
If X = 80 at the start (originally there were 80 reservations on the flight), N = 5 (T1 transfers 5 seat reservations from the
flight corresponding to X to the flight corresponding to Y), and M = 4 (T2 reserves 4 seats on X), the final result should be X
= 79. However, in the interleaving of operations shown in Figure (a), it is X = 84 because the update in T1 that removed the
five seats from X was lost
2. The Temporary Update (or Dirty Read) Problem:
This problem occurs when one transaction updates a database item
and then the transaction fails for some reason. Meanwhile, the updated item
is accessed (read) by another transaction before it ischanged back (or rolled
back) to its original value.
Figure (b) shows an example where T1 updates item X and then fails before completion, so the system must roll back X to
its original value. Before it can do so, however, transaction T2 reads the temporary value of X, which will not be recorded
permanently in the database because of the failure of T1. The value of item X that is read by T2 is called dirty data because
it has been created by a transaction that has not completed and committed yet; hence, this problem is also known as the
dirty read problem.
Types of Failures:-
Failures are generally classified as transaction, system, and media failures. Thereare several
possible reasons for a transaction to fail in the middle of execution:
execution. Hardware crashes are usually media failures for example,main memory failure.
5.Disk failure-
Some disk blocks may lose their data because of a read or write malfunction orbecause of a
disk read/write head crash. This may happen during a read or a write operation of the
transaction.
6.Physical problems and catastrophes-
This refers to an endless list of problems that includes power or air- conditioning failure, fire,
theft, sabotage, overwriting disks or tapes by mistake,and mounting of a wrong tape by the
operator.
Whenever a failure of type 1 through 4 occurs, the system must keep sufficient
information to quickly recover from the failure. Disk failure or other catastrophicfailures
of type 5 or 6 do not happen frequently; if they do occur, recovery is a major task.
TRANSACTION AND STATES AND ADDITIONAL
OPERATION
The following figure shows a state transition diagram that illustrates how a
transaction moves through its execution states. A transaction goes into an active
state immediately after it starts execution, where it can execute its READ and WRITE
operations. When the transaction ends, it moves to the partially committed state.
At this point, some types of concurrency control protocols may do
additional checks to see if the transaction can be committed or not.
If these checks are successful, the transaction is said to have reached its
commit point and enters the committed state. When a transaction is
committed, it has concluded its execution successfully and all its changes
must be recorded permanently in the database, even if a system failure
occurs.
However, a transaction can go to the failed state, if one of the checks fails
or if the transaction is aborted during its active state. The terminated
state corresponds to the transaction leaving the system.
SyStem log:
to recover from failures that affect transactions, the system maintains a log
.the memory area that holds data to be written to the log file on the disk
called the log buffers. When the log buffer is filled, or when certain other
conditions occur, the log buffer is appended to the end of the log file on disk.
T refers to a unique transaction-id that is generated automatically by the
system for each transaction and that is used to identify each transaction:
1. [start _transaction, T]: Indicates that transaction T has started
execution.
2. [write _item, T, X, old _value, new _value]:Indicates that transaction T
has changed the value of database item X from old _value to new _value.
3. [read _item, T, X]: Indicates that transaction T has read the value of
database item X.
4. [commit, T]: Indicates that transaction T has completed successfully,
and affirms that its effect can be committed (recorded permanently) to the
database.
5. [abort, T]: Indicates that transaction T has been aborted.
it is possible to undo the effect of these WRITE operations of a transaction T
by tracing backward through the log and resetting all items changed by a
WRITE operation of T to their old values. Redo of an operation may also be
necessary if a transaction has its updates recorded in the log but a failure
occurs before the system can be sure that all these new values have been
written to the actual database on disk from the main memory buffers.
For the purpose of recovery and concurrency control, we are mainly interested in the
read_item and write_item operations of the transactions, as well as the commit and
abort operations. A shorthand notation for describing a schedule uses the symbols b,
r, w, e, c, and a for the operations begin_transaction, read_item, write_item,
end_transaction, commit, and abort, respectively, and appends as a subscript the
transaction id (transaction number) to each operation in the schedule.
In this notation, the database item X that is read or written follows the r and w
operations in parentheses. In some schedules, we will only show the read and write
operations, whereas in other schedules we will show additional operations, such as
commit or abort.
Sa:R1(X);R2(X);W1(X);R1(Y);W2(X);W1(Y);
Sb:R1(X);W1(X);R2(X);W2(X);R1(Y)A1;
CONFLICTING OPERATION IN A SCHEDULE
Two operations in a schedule are said to conflict if they satisfy all three of the
following conditions:
1. They belong to different transaction;
2. They access the same item XZ; and
3. At least one of the operations is a write item(X);
For example, in schedule Sa, the operations r1(X) and w2(X) conflict, as do the
operations r2(X) and w1(X), and the operations w1(X) and w2(X). However, the
operations r1(X) and r2(X) do not conflict, since they are both read operations;
The operations w2(X) and w1(Y) do not conflict because they operate on distinct
data items X and Y; and the operations r1(X) and w1(X) do not conflict because
they belong to the same transaction. Intuitively, two operations are conflicting if
changing their order can result in a different outcome.
Here are some common types of conflicting operations.
1.Read-write conflict:
Transaction A reads a values, and then transaction B modifies
the same value before transaction A completes.
Example:
If we change the order of the two operations r1(X); w2(X) to w2(X); r1(X), then the
value of X that is read by transaction T1 changes, because in the second ordering the
value of X is read by r1(X) after it is changed by w2(X), whereas in the first ordering
the value is read before it is changed. This is called a read-write conflict
A: Read X
B: Write X (before A commits)
2.Write-write conflict:
These involve a combination of read and write operation on the same data item.
Example:
A:Read Z
B:Write Z
A single SQL statement is always considered to be atomic i.e., either the execution is completed
without an error (or) it fails and leaves the database unchanged.
With SQL, there is no Begin_Transaction statement. Transaction initiation is done when particular
SQL statements are encountered. But, every transaction must have an explicit end statement, which
is either a COMMIT or a ROLLBACK. Every transaction has certain characteristics to it, which are set
by the SET TRANSACTION statement in SQL.
If a transaction executes at a lower isolation level than SERIALIZABLE, then one or more of the
following three violations may occur:
• Dirty read
• Nonrepeatable read
• Phantoms
1. Dirty read
A transaction T1 may read the update of a transaction T2, which has not yet committed. If T2
fails and is aborted, then T1 would have read a value that does not exist and is incorrect.
2. Nonrepeatable read
A transaction T1 may read a given value from a table. If another transaction T2 updates the
value and T1 reads it again, T1 will see a different value.
3. Phantoms
A transaction T1 may read a set of rows from a table, based on some condition specified in the
SQL WHERE-clause. Now suppose that a transaction T2 inserts a new row r that also satisfies the
WHERE-clause condition used in T1. The record r is called a phantom record because it was not
there when T1 starts but is there when T1 ends. T1 may or may not see the phantom row. If the
equivalent serial order is T1 followed by T2, then the record r should not be seen; but if it is T2
followed by T1, then the phantom record should be in the result given to T1. If the system
cannot ensure the correct behaviour, then it does not deal with the phantom record problem.
Type of Violation
Isolation Level Dirty Read Non repeatable read Phantom
READ UNCOMMITTED YES YES YES
READ COMMITTED NO YES YES
REPEATABLE READ NO NO YES
SERIALIZABLE NO NO NO
The above table summarizes the possible violations for the different isolation levels.
• An entry of Yes indicates that a violation is possible and an entry of No indicates that it is not
possible.
• READ UNCOMMITTED is the most forgiving, and SERIALIZABLE is the most restrictive in that
it avoids all three of the problems mentioned above.
Snapshot Isolation
• Snapshot isolation is used in some commercial DBMSs, and some concurrency control
protocols exist that are based on this concept.
• The basic definition of snapshot isolation is that a transaction sees the data items that it
reads based on the committed values of the items in the database snapshot (or
database state) when the transaction starts.
• Snap shot isolation will ensure that the phantom record problem does not occur, or in
some cases the database statement will only see the records that were inserted in the
database at the time the transaction starts.
• Any insertions, deletions, or updates that occur after the transaction starts will not be
seen by the transaction.
Questions:
1. Describe the four levels of isolation in SQL. Also discuss the concept of snapshot isolation
and its effect on the phantom record problem.
2. Define the violations caused by each of the following: dirty read, nonrepeat able read, and
phantoms