Professional Documents
Culture Documents
3.1 Transaction Concepts
3.1 Transaction Concepts
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 indivisible, it either executes in its entirety or not at all
TRANSACTION CONCEPTS | ACID PROPERTIES
Atomicity : Either all operations of the transaction are reflected properly in the database, or none
are
Consistency : Execution of a transaction in isolation (that is, with no other transaction executing
concurrently) preserves the consistency of the database.
Isolation : Even though multiple transactions may execute concurrently, the system guarantees that,
for every pair of transactions Ti and Tj , it appears to Ti that either Tj finished execution before Ti
started or Tj started execution after Ti finished. Thus, each transaction is unaware of other
transactions executing concurrently in the system
Durability : After a transaction completes successfully, the changes it has made to the database
persist, even if there are system failures.
TRANSACTION CONCEPTS | TRANSACTION MODEL
read(X): which transfers the data item X from the database to a variable, also called X, in a buffer
in main memory belonging to the transaction that executed the read operation.
write(X): which transfers the value in the variable X in the main-memory buffer of the transaction
that executed the write to the data item X in the database.
Let Ti be a transaction that transfers $50 from account A to account B. This transaction can be
defined as
Ti : read(A);
A := A −
50;
write(A);
read(B);
B := B +
50;
write(B).
TRANSACTION CONCEPTS | TRANSACTION MODEL
Ti : read(A);
A := A −
50;
write(A);
read(B);
B := B +
50;
write(B).
• If the transaction fails after completion of T1 but before completion of T2.(say, after write(X) but
before write(Y)), then amount has been deducted from X but not added to Y. This results in an
inconsistent database state. Therefore, the transaction must be executed in entirety in order to ensure
correctness of database state.
TRANSACTION CONCEPTS | ACID PROPERTIES
Consistency
• This means that integrity constraints must be maintained so that the database is consistent before and after
the transaction.
Isolation
This property ensures that the execution of transactions concurrently will result in a state that is equivalent to a
state achieved these were executed serially in some order.
Let X= 500, Y = 500.
Consider two transactions T and T”.
Suppose T has been executed till Read (Y) and then T’’ starts. As a result , interleaving of operations takes place
due to which T’’ reads correct value of X but incorrect value of Y and sum computed by
T’’: (X+Y = 50, 000+500=50, 500)
is thus not consistent with the sum at end of transaction:
T: (X+Y = 50, 000 + 450 = 50, 450).
This results in database inconsistency, due to a loss of 50 units. Hence, transactions must take place in isolation
TRANSACTION CONCEPTS | ACID PROPERTIES
Durability
• This property ensures that once the transaction has completed execution, the updates and modifications to
the database are stored in and written to disk and they persist even if a system failure occurs. These updates
now become permanent and are stored in non-volatile memory. The effects of the transaction, thus, are
never lost.
Transaction Concepts | Transaction Atomicity And Durability
A transaction may not always complete its execution successfully. Such a transaction is termed
aborted
Once the changes caused by an aborted transaction have been undone, we say that the transaction has
been rolled back
It is part of the responsibility of the recovery scheme to manage transaction aborts. This is done
typically by maintaining a log.
The only way to undo the effects of a committed transaction is to execute a compensating transaction
Active, the initial state; the transaction stays in this state while it is executing.
Partially committed, after the final statement has been executed.
Failed, after the discovery that normal execution can no longer proceed
Aborted, after the transaction has been rolled back and the database has been restored to its state prior
to the start of the transaction.
Committed, after successful completion
Transaction Concepts | Transaction Atomicity And Durability