Transactions &ConcurrencyControl
Transactions &ConcurrencyControl
in Distributed Database
Prepared By :
Prabesh Khadka
Joseph Nakarmi
Contents
Transaction Concept
Properties of Transaction
Type Of Transaction
Architecture Revisited
Concurrency Control
Serializibilty
Concurrency Control Algorithm
Transaction Concept
A transaction is a unit of program execution
that accesses and possibly updates various
data items
A transaction must see a consistent database
During transaction execution the database
may be temporarily inconsistent
Multiple transactions can execute in parallel
Transaction Recovery
A transaction is considered a logical unit of work
Start Statement: BEGIN TRANSACTION
END Statement: COMMIT
Execution errors: ROLLBACK
COMMIT establishes a Commit Point or Synch
Point.
ROLLBACK has to roll back the database to the
state it had before the Transaction started.
Contd.
BEGIN TRANSACTION
UPDATE ACCOUNT_A {BALANCE = BALANCE -100}
IF any error occurred then GO TO UNDO; END IF;
UPDATE ACCOUNT_B {BALANCE = BALANCE
+100}
IF any error occurred then GO TO UNDO; END IF;
COMMIT;
GO TO FINISH;
UNDO:
ROLLBACK; FINISH:
RETURN;
Characterization
Read set (RS)
The set of data items that are read by a transaction
Write set (WS)
The set of data items whose values are changed by this
transaction
Base set (BS)
RS ∪ WS
Transaction ACID Properties
To preserve the integrity of data the database system transaction
mechanism must ensure:
Atomicity
Transactions are atomic (all or nothing)
Consistency
Transaction transform the DB from one correct state to another correct
state
Isolation
Transactions are isolated from each other
Durability
Once a transaction commits, changes are permanent: no subsequent
failure can reverse the effect of the transaction
Transaction 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
Contd.
Aborted
• after the transaction has been rolled back and the
database restored to its state prior to the start of
the transaction
• Two options after it has been aborted:
Restart the transaction; can be done only
if no internal logical error occurred
Kill the transaction
Committed
after successful completion
Characterization of Transactions
Based on
Application areas
Non-distributed vs. distributed
Compensating transactions
Heterogeneous transactions
Timing
On-line (short-life) vs batch (long-life)
Organization of read and write actions
Two-step
Restricted
Action model
Structure
Flat (or simple) transactions
Nested transactions
Workflows
Transaction Structure
Flat transaction
Consists of a sequence of primitive operations embraced between a begin and
end markers.
Begin_transaction Reservation
…
end.
Nested transaction
The operations of a transaction may themselves be transactions.
Begin_transaction Reservation
…
Begin_transaction Airline
…
end. {Airline}
Begin_transaction Hotel
…
end. {Hotel}
end. {Reservation}
Nested Transactions
Have the same properties as their parents may themselves
have other nested transactions.
Introduces concurrency control and recovery concepts to
within the transaction.
Types
Closed nesting
Sub transactions begin after their parents and finish before them.
Commitment of a sub transaction is conditional upon the commitment of
the parent (commitment through the root).
Open nesting
Sub transactions can execute and commit independently.
Compensation may be necessary.
Workflows
“A collection of tasks organized to accomplish some business
process.”
Types
Human-oriented workflows
Involve humans in performing the tasks.
System support for collaboration and coordination; but no system-wide consistency
definition
System-oriented workflows
Computation-intensive & specialized tasks that can be executed by a computer
System support for concurrency control and recovery, automatic task execution,
notification, etc.
Transactional workflows
In between the previous two; may involve humans, require access to
heterogeneous, autonomous and/or distributed systems, and support selective use
of ACID properties
Transactions Provide…
Atomic and reliable execution in the presence of
failures
Correct execution in the presence of multiple user
accesses
Correct management of replicas (if they support it)
Transaction Processing Issues
Transaction structure (usually called transaction model)
Reliability protocols
Distributed
Execution Monitor
Transaction Manager
(TM)
With other With other
Scheduling/
TMs Descheduling SCs
Requests
Scheduler
(SC)
To data
processor
Centralized Transaction Execution
User User
Application Application
…
Begin_Transaction, Results &
Read, Write, Abort, EOT User Notifications
Transaction
Manager
(TM)
Read, Write,
Results
Abort, EOT
Scheduler
(SC)
Scheduled
Results
Operations
Recovery
Manager
(RM)
Distributed Transaction Execution
User application
Local
RM RM Recovery
Protocol
Concurrency Control
Lock point
Obtain lock
Release lock
No. of locks
Phase 1 Phase 2
BEGIN END
Two-Phase Locking (2PL)
In a distributed system there are sites designated as
Lock managers
At least one site is given the authority to see all
transactions and detect lock conflicts
Two-phase locking approaches can be of following
types:
1. Centralized two-phase locking
2. Distributed two-phase locking
Centralized 2PL
There is only one 2PL scheduler in the distributed system.
Lock requests are issued to the central scheduler(Lock manager).
Data Processors at
participating sites Coordinating TM Central Site LM
Lock
R eques
t
Gra nt ed
Lock
pe r ation
O
End of
O peratio
n
Releas
e Locks
Distributed 2PL
Schedulers are placed at each site.
Ti Tj
Deadlock Management
Prevention
Guaranteeing that deadlocks can never occur in the first
place. Check transaction when it is initiated. Requires no run
time support.
Avoidance
Detecting potential deadlocks in advance and taking action
to insure that deadlock will not occur. Requires run time
support.
Detection and Recovery
Allowing deadlocks to form and then finding and breaking
them. As in the avoidance scheme, this requires run time
support.
Deadlock Prevention
Do not allow any transaction to acquire locks that will
lead to deadlocks
All resources which may be needed by a transaction
must be pre-declared.
The system must guarantee that none of the resources
will be needed by an ongoing transaction.
Resources must only be reserved, but not necessarily
allocated a priori
Unsuitability of the scheme in database environment
Deadlock Avoidance
The deadlock avoidance approach handles deadlocks
before they occur
Transactions are allowed to proceed unless a requested
resource is unavailable.
In case of conflict, transactions may be allowed to wait
for a fixed time interval.
More attractive than prevention in a database
environment.
Deadlock Avoidance –
Wait-Die Algorithm