unit5
unit5
Transaction:
A transaction is a unit of program execution that accesses and possibly
updates various data items.
Usually, a transaction is initiated by a user program written in a high-level
data-manipulation language (typically SQL), or programming language with
embedded database accesses in JDBC or ODBC.
A transaction is delimited by statements (or function calls) of the form begin
transaction and end transaction.
The transaction consists of all operations executed between the begin
transaction and end transaction.
A transaction is action, or series of actions, carried out by user or application,
which accesses or updates contents of database.
It Transforms database from one consistent state to another, although
consistency may be violated during transaction.
The concept of transaction provides a mechanism for describing logical units of
database processing.
Transaction processing systems are systems with large databases and
hundreds of concurrent users that are executing database transactions.
Examples of such systems include systems for reservations, banking, stock
markets, super markets and other similar systems.
They require high availability and fast response time for hundreds of
concurrent users.
Step 4 is the one that actually updates the database on disk. In some cases the
buffer is not immediately stored to disk, in case additional changes are to be
made to the buffer.
Usually, the decision about when to store back a modified disk block that is in
a main memory buffer is handled by the recovery manager of the DBMS in
cooperation with the underlying operating system.
The DBMS will generally maintain a number of buffers in main memory that
hold database disk blocks containing the database items being processed
A transaction includes read_item and wri te_item operations to access and
update the database. Figure 4.2 shows examples of two very simple
transactions.
The read-set of a transaction is the set of all items that the transaction reads,
and the write-set is the set of all items that the transaction writes.
Fig 4.2: Two sample transactions. (a) Transaction T1 . (b) Transaction T2
Jim Gray defined these properties of a reliable transaction system in the late
1970s and developed technologies to achieve them automatically
Atomicity:
Atomicity refers to the ability of the DBMS to guarantee that either all of the
operations of a transaction are performed or none of them are. Database
modifications must follow an all or nothing rule. Each transaction is said to be
atomic if when one part of the transaction fails, the entire transaction fails.
The atomicity property requires that we execute a transaction to completion. It
is the responsibility of the transaction recovery subsystem of a DBMS to ensure
atomicity.
If a transaction fails to complete for some reason, such as a system crash in
the midst of transaction execution, the recovery technique must undo any
effects of the transaction on the database.
Consistency:
The consistency property ensures that the database remains in a consistent
state before the start of the transaction and after the transaction is over
(whether successful or not).
The preservation of consistency is generally considered to be the responsibility
of the programmers who write the database programs or of the DBMS module
that enforces integrity constraints.
A consistent state of the database satisfies the constraints specified in the
schema as well as any other constraints that should hold on the database. A
database program should be written in a way that guarantees that, if the
database is in a consistent state before executing the transaction, it will be in a
consistent state after the complete execution of the transaction,
Isolation:
The isolation portion of the ACID Properties is needed when there are
concurrent transactions. Concurrent transactions are transactions that occur
at the same time, such as shared multiple users accessing shared objects.
Although multiple transactions may execute concurrently, each transaction
must be independent of other concurrently executing transactions. A
transaction should appear as though it is being executed in isolation from
other transactions. That is, the execution of a transaction should not be
interfered with by any other transactions executing concurrently.
In a database system where more than one transaction are being executed
simultaneously and in parallel, the property of isolation states that all the
transactions will be carried out and executed as if it is the only transaction in
the system. No transaction will affect the existence of any other transaction.
Durability:
recovery to the most recent successful commit after a data disk failure
Schedule
When transactions are executing concurrently in an interleaved fashion, then
the order Of execution of operations from the various transactions is known as
a schedule.
A sequences of instructions that specify the chronological order in which
instructions of concurrent transactions are executed
Schedules (Histories) of Transactions
A schedule (or history) S of n transactions T1 , T2, ... , Tn is an ordering of the
operations of the transactions subject to the constraint that, for each
transaction Ti that participates in S, the operations of Ti, in S must appear in
the same order in which they occur in Ti.
That means a schedule for a set of transactions must consist of all instructions
of those transactions.
For example, the schedule of below Figure which we shall call Sa can be written
as follows in this notation:
Sa: r1(X); r2(X); W1(X); r1(Y); w2(X); W1(Y);
Two operations in a schedule are said to conflict if they satisfy all three of the
following conditions:
(l) they belong to different transactions;
(2) they access the same item X; and
(3) at least one of the operations is a write_item(X).
For example, in schedule Sa the operations r l(X) and w2(X) , the operations
r2(X)and W1(X), and the operations w1(X) and W2(X) are conflict. However, the
operations r l(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 rl(X)and W1 (X) do not conflict,
because they belong to the same transaction.
However,
consider the two (schedules Sc and Sd that follow:
Sc: rl(X); W1(X); r2(X); r1(Y); w2(X); C2; al;
Sd: rl (X); W1 (X); r2(X); rl(Y); w2(X); W1(Y); C1; C2;
Se: rl(X); W1(X); T2(X); rl(Y); W2(X); W1(Y); a1; a2;
Sc is not recoverable, because T2 reads item X from T1 , and then T2 commits
before T1 commits. If TI aborts after the C2 operation in Sc then the value of X
that T2 read is no longer valid and T2 must be aborted after it had been
committed, leading to a schedule that is not recoverable. For the schedule to be
recoverable, the C2 operation in Sc must be postponed until after T I commits,
as shown in Sd; if T I aborts instead of committing, then
T2 should also abort as shown in Se' because the value of X it read is no longer
valid.
2. Cascadeless schedule
A schedule is said to be cascadeless, if every transaction in the schedule reads
only items that were written by committed transactions.
To satisfy this criterion, the r2(X) command in schedules Sd and Se must be
postponed until after T1 has committed or aborted.
3. Strict schedules
A schedule, called a strict schedule, in which transactions can neither read
nor write an item X until the last transaction that wrote X has committed (or
aborted). Strict schedules simplify the recovery process.
Serial schedule:
Goal: Like to find non-serial schedules which are also correct, because in serial
schedules one transaction have to wait for another transaction to complete,
Hence serial schedules are unacceptable in practice.
Result equivalent:
Two schedules are called result equivalent if they produce
the same final state of the database.
S1 S2
read_item(X); read_item(X);
X:=X+10; X:=X*1.1;
write_item(X); write_item(X);
Conflict equivalent:
Conflict serializable:
Non-conflicting operations:
FIGURE 17.7 Constructing the precedence graphs for schedules A and D from
Figure 17.5 to test for conflict serializability.
View equivalence:
A less restrictive definition of equivalence of schedules
View serializability:
Two schedules are said to be view equivalent if the following three conditions
hold:
In Sa, the operations w2(X) and w3(X) are blind writes, since T1 and T3 do not
read the value of X.
Sa is view serializable, since it is view equivalent to the serial schedule T1, T2,
T3. However, Sa is not conflict serializable, since it is not conflict equivalent
to any serial schedule.
Introduction to Concurrency
What is concurrency?
LOCK
Definition : Lock is a variable associated with data item which gives the
status whether the possible operations can be applied on it or not.
• Lock table:
Lock manager uses it to store the identify of transaction locking
a data item, the data item, lock mode . One simple way to
implement a lock table is through linked list.
else begin
goto B
end;
– a.k.a. Shared/Exclusive
• Three operations
– read_lock(X)
– write_lock(X)
– unlock(X)
• Each data item can be in one of three lock states
– Three locks modes:
• (a) shared (read) (b) exclusive (write) (c) unlock(release)
– Shared mode: shared lock (X)
More than one transaction can apply share lock on X for
reading its value but no write lock can be applied on X by any
other transaction.
The transaction T
Lock table:
This protocol isn’t enough to guarantee serializability. If locks are released too
early, you can create problems. This usually happens when a lock is released
before another lock is acquired.
no_of_reads (X) 1;
end
go to B
end;
go to B
end;
end
begin
begin
end
end;
Lock conversion:-----
else
Ti has a write-lock (X) (*no transaction can have any lock on X*)
– Downgrading ok
– Upgrading is not
Rules of 2PL are as follows:
But, it may limit the amount of concurrency since transactions may have to
hold onto locks longer than needed, creating the new problem of deadlocks.
T1 T2 Result
read_lock (X);
read_item (X);
unlock (X);
write_lock (Y);
read_item (Y);
Y:=X+Y;
write_item (Y);
unlock (Y);
write_lock (X);
read_item (X);
X:=X+Y;
write_item (X);
unlock (X);
X:=X+Y; Y:=X+Y;
1. BASIC
2. CONSERVATIVE
3. STRICT
4. RIGOUROUS