UNIT5 Transaction Processing and Concurrency Control
UNIT5 Transaction Processing and Concurrency Control
TRANSACTION
PROCESSING
AND
CONCURRENCY
CONTROL
Prof. Sandipkumar C. Sagare
CONTENTS
◦ Transaction Processing – Concept
◦ ACID properties
◦ Transaction model
◦ Schedule
◦ Serializability – conflict and view Serializability,
◦ Recoverable schedule
◦ Concurrency Control Mechanisms –
◦ Lock based protocols
◦ Multiple Granularity
◦ Timestamp based protocols
◦ Thomas’s Write Rule
◦ Validation based protocols
Transaction Processing
Transaction - Introduction
• a collection of several operations on the database
appears to be a single unit from the point of view
of the database user.
• For example, a transfer of funds from a checking
account to a savings account is a single operation
from the customer’s standpoint;
• within the database system, it consists of several
operations.
• it is essential that all these operations occur,
• or in case of a failure, none occur.
• It would be unacceptable if the checking account
were debited, but the savings account were not
credited.
• Collections of operations that form a single logical
unit of work are called transactions.
• A database system must ensure proper execution
of transactions despite failures
• either the entire transaction executes, or none of
it does.
• it must manage concurrent execution of
transactions in a way that avoids the introduction
of inconsistency.
Transaction Concept
• 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 or programming language (for example,
SQL, COBOL, C, C++, or Java)
• The transaction consists of all operations
executed between the begin transaction and end
transaction.
• To ensure integrity of the data,
• It is required that the database system maintain
the following properties of the transactions:
• Atomicity
• Consistency
• Isolation
• Durability
• 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 State
• In the absence of failures, all transactions must
complete successfully.
• a transaction may not always complete its
execution successfully.
• Such a transaction is termed aborted.
• To ensure the atomicity property, an aborted
transaction must have no effect on the state of
the database.
• any changes that the aborted transaction made to
the database must be undone.
• 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.
• A transaction that completes its execution
successfully is said to be committed.
• A committed transaction that has performed
updates
• transforms the database into a new consistent
state,
• which must persist even if there is a system
failure.
• Once a transaction has committed,
• we cannot undo its effects by aborting it.
• The only way to undo the effects of a committed
transaction is to execute a compensating
transaction.
A simple abstract transaction model
• A transaction must be in one of the following
states:
• 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
• Two options after it has been aborted:
• restart the transaction –only if no internal logical error
• kill the transaction
• Committed
• after successful completion
• The state diagram corresponding to a transaction
appears in Figure
• We say that a transaction has committed only if it
has entered the committed state.
• Similarly, we say that a transaction has aborted
only if it has entered the aborted state.
• A transaction is said to have terminated if has
either committed or aborted
Implementation of Atomicity and Durability
• The recovery-management component of a
database system implements the support for
atomicity and durability.
The shadow-database scheme:
• The shadow-database scheme:
– assume that only one transaction is active at a
time.
– a pointer called db_pointer always points to the
current consistent copy of the database.
– all updates are made on a shadow copy of the
database, and db_pointer is made to point to the
updated shadow copy only after the transaction
reaches partial commit and all updated pages
have been flushed to disk.
– in case transaction fails, old consistent copy
pointed to by db_pointer can be used, and the
shadow copy can be deleted.
Concurrent Executions
• Multiple transactions are allowed to run
concurrently in the system.
• Advantages are:
• increased processor and disk utilization
leading to better transaction throughput:
one transaction can be using the CPU while
another is reading from or writing to the disk
transaction transaction
with smaller with larger
timestamp timestamp
• Solution:
– A transaction is structured such that its writes are all
performed at the end of its processing
– All writes of a transaction form an atomic action; no
transaction may execute while a transaction is being
written
– A transaction that aborts is restarted with a new
timestamp
Thomas’ write rule
• We now present a modification to the timestamp-
ordering protocol
• that allows greater potential concurrency
• Let us consider a schedule
• 3. Otherwise,
• the system executes the write operation and sets
W-timestamp(Q) to TS(Ti).
• The difference between these rules and those of
timestamp-ordering protocol lies in the second
rule.
• The timestamp-ordering protocol requires that Ti
be rolled back if Ti issues write(Q) and
TS(Ti)<W-timestamp(Q).
• Timestamp of Current transaction(Ti) is greater
than the transaction recently read that data Q
• Thomas’ write rule makes use of view
serializability by, in effect,
• deleting obsolete write operations from the
transactions that issue them.
• Thomas' Write Rule allows greater potential
concurrency.
Validation-Based Protocol
• In cases where a majority of transactions are
read-only transactions,
• the rate of conflicts among transactions may be
low.
• Thus, many of these transactions, if executed
without the supervision of a concurrency-control
scheme,
• would not leave the system in a in-consistent
state.
Validation-Based Protocol
• A concurrency-control scheme imposes overhead
of code execution and possible delay of
transactions.
• It may be better to use an alternative scheme that
imposes less overhead.
• A difficulty in reducing the overhead is that we do
not know in advance which transactions will be
involved in a conflict.
• To gain that knowledge, we need a scheme for
monitoring the system.
Validation-Based Protocol
• We assume that each transaction Ti executes in
three different phases in its lifetime,
• depending on whether it is a read-only or an
update transaction.
• The phases are, in order,
• 1. Read phase.
• During this phase, the system executes
transaction Ti.
• It reads the values of the various data items and
stores them in variables local to Ti.
• It performs all write operations on temporary local
variables, without updates of the actual database.
• 2. Validation phase
• Transaction Ti performs a validation test to
determine
• whether it can copy to the database the
temporary local variables
• that hold the results of write operations without
causing a violation of serializability.
• 3. Write phase.
• If transaction Ti succeeds in validation (step 2),
• then the system applies the actual updates to the
database.
• Otherwise, the system rolls back Ti.
• The three phases of concurrently executing
transactions can be interleaved,
• but each transaction must go through the three
phases in that order.
• Also called as optimistic concurrency
control
• since transaction executes fully in the hope
that all will go well during validation
• Each transaction Ti has 3 timestamps
− Start(Ti) : the time when Ti started its execution
− Validation(Ti): the time when Ti entered its validation
phase
− Finish(Ti) : the time when Ti finished its write phase
• Serializability order is determined by timestamp given
at validation time, to increase concurrency.
• Thus TS(Ti) is given the value of Validation(Ti).
• This protocol is useful and gives greater degree of
concurrency if probability of conflicts is low.
• That is because the serializability order is not pre-
decided and relatively less transactions will have to be
rolled back.
• Thus, the value TS(Ti) = Validation(Ti)
• and, if TS(Tj)<TS(Tk),
• then any produced schedule must be equivalent
to a serial schedule in which transaction Tj
appears before transaction Tk.
• The reason we have chosen Validation(Ti), rather
than Start(Ti), as the timestamp of transaction Ti
is that
• we can expect faster response time provided that
conflict rates among transactions are indeed low.
• Validation Test for Transaction Tj
• The validation test for transaction Tj requires that,
• for all transactions Ti with TS(Ti)<TS(Tj),
• one of the following two conditions must hold:
• 1. Finish(Ti)<Start(Tj).
• SinceTi completes its execution before Tj started,
• the serializability order is maintained
• 2. The set of data items written by Ti does not
intersect with the set of data items read by Tj, and
• Ti completes its write phase before Tj starts its
validation phase
(Start(Tj)<Finish(Ti)<Validation(Tj))
• This condition ensures that the writes of Ti and Tj
do not overlap.
• Since the writes of Ti do not affect the read of Tj,
and since Tj cannot affect the read of Ti, the
serializability order is maintained.
Multiple Granularity
• Allow data items to be of various sizes
• and define a hierarchy of data granularities,
• where the small granularities are nested within
larger ones
• Can be represented graphically as a tree
• When a transaction locks a node in the tree
explicitly, it implicitly locks all the node's
descendants in the same mode.
Multiple Granularity
• Granularity of locking
• (level in tree where locking is done):
– fine granularity (lower in tree):
– high concurrency, high locking overhead