Unit2 2
Unit2 2
Consider the following SQL query for increasing by 10% the budget of the
CAD/CAM project that we discussed:
UPDATE PROJ
SET BUDGET = BUDGET * 1.1
WHERE PNAME = “CAD/CAM”
Begin_transaction Reservation
begin
input(flight_no, date, customer_name);
EXEC SQL SELECT STSOLD,CAP
INTO temp1,temp2
FROM FLIGHT
WHERE FNO = flight_no AND DATE = date;
if temp1 = temp2 then
output(“no free seats”);
Abort
else
EXEC SQL UPDATEFLIGHT
SET STSOLD = STSOLD + 1
WHERE FNO = flight_no AND DATE = date;
EXEC SQL INSERT
INTO FC(FNO, DATE, CNAME, SPECIAL);
VALUES(flight_no, date, customer_name, null);
Commit
output(“reservation completed”)
end if
end . {Reservation}
Example..
Begin_transaction
Reservation
begin
input(flight_no, date, customer_name);
temp <- Read(flight_no(date).stsold);
if temp = flight(date).cap then
begin
output(“no free seats”);
Abort
end
else
begin
Write(flight(date).stsold, temp + 1);
Write(flight(date).cname, customer_name);
Write(flight(date).special, null);
Commit;
output(“reservation completed”)
end
end
Reservation
Properties of Transaction…
Atomicity [ALL OR NOTHING]
Either all or none of the transaction's operations are performed.
Isolation
Serializability
• If several transactions are executed concurrently,
the results must be the same as if they were
executed serially in some order.
Incomplete results
oAn incomplete transaction cannot reveal its
results to other transactions before its
commitment.
oNecessary to avoid cascading aborts.
Properties of Transaction…
Durability
Once a transaction commits, the system
must guarantee that the results of its
operations will never be lost, in spite of
subsequent failures.
Database recovery
Centralized Transaction Execution
Distributed Transaction Execution
Transaction Manager
Record Manager
Scheduler
Concurrency Control
Concurrency Control…
Several Problems….
Anomalies:
Lost updates
The effects of some transactions are not reflected on the
database.
Inconsistent retrievals
A transaction, if it reads the same data item more than
once, should always read the same value.
Concurrency Control…
Scheduling Algorithms…
Majority Protocol
Local lock manager at each site administers
lock and unlock requests for data items stored
at that site.
When a transaction wishes to lock an un
replicated data item Q residing at site Si, a
message is sent to Si ‘s lock manager.
If Q is locked in an incompatible mode, then the
request is delayed until it can be granted.
When the lock request can be granted, the lock
manager sends a message back to the initiator
indicating that the lock request has been granted.
Majority Protocol (Cont.)
• In case of replicated data
– If Q is replicated at n sites, then a lock request message must be sent to more
than half of the n sites in which Q is stored.
– The transaction does not operate on Q until it has obtained a lock on a majority
of the replicas of Q.
– When writing the data item, transaction performs writes on all replicas.
• Benefit
– Can be used even when some sites are unavailable
• Drawback
– Requires 2(n/2 + 1) messages for handling lock requests, and (n/2 + 1) messages
for handling unlock requests.
– Potential for deadlock even with single item - e.g., each of 3 transactions may
have locks on 1/3rd of the replicas of a data.
Biased Protocol…
Centralized 2PL.
Distributed 2PL.
Centralized 2Phase Locking…
Distributed Phase Locking …
Timestamp Ordering…
If T wants to write(X)