When two transactions run concurrently which types of problems encounter are?
Note that serial execution doesn't mean that each transaction will get the same results, regardless of the order. Show Consider the following two transactions: T1: select sum(salary) from Employee where dept='Sales' T2: insert into Employee values (....,'Sales',...) If we execute T1: R(X) W(X) R(Y) W(Y) T2: R(X) W(X)5then T1: R(X) W(X) R(Y) W(Y) T2: R(X) W(X)6, we get a smaller salary total than if we execute T1: R(X) W(X) R(Y) W(Y) T2: R(X) W(X)6then T1: R(X) W(X) R(Y) W(Y) T2: R(X) W(X)5. In both cases, however, the salary total is consistent with the state of the database at the time the query is executed. A serial execution of consistent transactions is always consistent. If transactions execute under a concurrent (nonserial) schedule, the potential exists for conflict among their effects. In the worst case, the effect of executing the transactions ...
Valid Concurrent Transaction Not all concurrent executions cause problems. For example, the schedules T1: R(X) W(X) R(Y) W(Y) T2: R(X) W(X) or T1: R(X) W(X) R(Y) W(Y) T2: R(X) W(X) or ... leave the database in a consistent state. Consider the following schedule where the transactions execute in parallel: T1: ... A B C ... T2: ... A B C ...0 In this scenario:
The result: T1: R(X) W(X) R(Y) W(Y) T2: R(X) W(X)5's update to Database T1 T2 --------- ------------------- -------------- X Y X Y X 100 50 ? ? ? read(X) 100 X:=X+N 105 105 write(X) read(Y) 50 Y:=Y-N 45 45 write(Y) read(X) 105 X:=X+M 113 113 write(X) --------- 113 452is lost. Lost Update Problem (cont) Consider the states in the WR Conflict schedule: T1: ... A B C ... T2: ... A B C ...1 Consider the following schedule where one transaction fails: T1: ... A B C ... T2: ... A B C ...2 Transaction T1: R(X) W(X) R(Y) W(Y) T2: R(X) W(X)5aborts after writing Database T1 T2 --------- ------------------- -------------- X Y X Y X 100 50 ? ? ? read(X) 100 X:=X+N 105 105 write(X) read(Y) 50 Y:=Y-N 45 45 write(Y) read(X) 105 X:=X+M 113 113 write(X) --------- 113 452. The abort will undo the changes to Database T1 T2 --------- ------------------- -------------- X Y X Y X 100 50 ? ? ? read(X) 100 X:=X+N 105 105 write(X) read(Y) 50 Y:=Y-N 45 45 write(Y) read(X) 105 X:=X+M 113 113 write(X) --------- 113 452, but where the undo occurs can affect the results. Consider three places where undo might occur: T1: ... A B C ... T2: ... A B C ...3 Temporary Update - Case 1 This scenario is ok. T1: R(X) W(X) R(Y) W(Y) T2: R(X) W(X)5's effects have been eliminated. T1: ... A B C ... T2: ... A B C ...4 Temporary Update - Case 2 In this scenario, some of T1: R(X) W(X) R(Y) W(Y) T2: R(X) W(X)5's effects have been retained. T1: ... A B C ... T2: ... A B C ...5 Temporary Update - Case 3 In this scenario, T1: R(X) W(X) R(Y) W(Y) T2: R(X) W(X)6's effects have been lost, even after commit. T1: ... A B C ... T2: ... A B C ...6 For ACID, we must ensure that schedules are:
If a concurrent schedule for transactions T1 ..Tn acts like a serial schedule for T1 ..Tn, then consistency is guaranteed. To determine this requires a notion of schedule equivalence. Note: we are not attempting to determine equivalence of entire computations, simply of the interleaved sequences of read/write operations. A serializable schedule is a concurrent schedule that produces a final state that is the same as that produced by some serial schedule. There are two primary formulations of serializability:
Consider two transactions T1 and T2 acting on data item X. Considering only read/write operations, the possibilities are: T1 firstT2 firstEquiv?R1(X) R2(X)R2(X) R1(X)yesR1(X) W2(X)W2(X) R1(X)noW1(X) R2(X)R2(X) W1(X)noW1(X) W2(X)W2(X) W1(X)no If T1 and T2 act on different data items, result is equivalent regardless of order. Conflict Serializability (cont) Two transactions have a potential conflict if
Conversely, if two operations in a schedule don't conflict, This gives a basis for determining equivalence of schedules. Conflict Serializability (cont) If we can transform a schedule
If a concurrent schedule is equivalent to some (any) serial schedule, then we have a consistency guarantee. Conflict Serializability (cont) Example: transform a concurrent schedule to serial schedule T1: ... A B C ... T2: ... A B C ...7 View Serializability is
View Serializability (cont) Two schedules S and S' on T1 .. Tn are view equivalent iff
In practice, we don't test specific schedules for serializability. However, in designing concurrency control schemes, we need a way of checking whether they produce "safe" schedules. This is typically achieved by a demonstration that the scheme generates only serializable schedules, and we need a serializability test for this. There is a simple and efficient test for conflict serializability; Both tests are based on notions of
Testing Serializability (cont) A precedence graph G = (V,E) for a schedule S consists of
Testing Serializability (cont) If an edge Tj → Tk exists in the precedence graph
Thus, the serializability test is reduced to cycle-detection (and there are cycle-detection algorithms available in many algorithms textbooks) Serializability Test Examples Serializable schedule (with conflicting operations shown in red): T1: ... A B C ... T2: ... A B C ...8 Precedence graph for this schedule: No cycles ⇒ serializable (as we already knew) Serializability Test Examples (cont) Consider this schedule: T1: ... A B C ... T2: ... A B C ...9 Precedence graph for this schedule: Has a cycle ⇒ not serializable Serializability Test Examples (cont) Consider this 3-transaction schedule: T1: read(X) T2: read(X) X := X + N X := X + M write(X) write(X) read(Y) Y := Y - N write(Y)0 Precedence graph for this schedule: No cycles ⇒ serializable Serializability Test Examples (cont) Consider this 3-transaction schedule: T1: read(X) T2: read(X) X := X + N X := X + M write(X) write(X) read(Y) Y := Y - N write(Y)1 Precedence graph for this schedule: Has a cycle ⇒ not serializable Having serializability tests is useful theoretically, but they do not provide a practical tool for organising schedules. Why not practical?
Concurrency Control (cont) Approaches to ensuring ACID transactions:
Lock-based Concurrency Control Synchronise access to shared data items via following rules:
To guarantee serializability, we require an additional constraint on how locks are applied:
Appropriate locking can guarantee correctness. However, it also introduces potential undesirable effects:
Deadlock occurs when two transactions are waiting for a lock on an item held by the other. Example: T1: read(X) T2: read(X) X := X + N X := X + M write(X) write(X) read(Y) Y := Y - N write(Y)2 Handling deadlock involves forcing a transaction to "back off".
Starvation occurs when one transaction
Multiple locks ⇒ need to decide which to release first. Solutions:
Locking typically reduces concurrency ⇒ reduces throughput. Granularity of locking can impact performance: + lock a small item ⇒ more of database accessible + lock a small item ⇒ quick update ⇒ quick lock release - lock small items ⇒ more locks ⇒ more lock management Granularity levels: field, row (tuple), table, whole database Multiple lock-granularities give best scope for optimising performance. Multi-version Concurrency Control One approach to reducing the requirement for locks is to
The primary difference between MVCC and standard locking models:
Database systems using MVCC ensure
PostgreSQL uses MVCC to reduce locking requirements. Consequences:
Concurrency control is still needed (via implicit locking):
PostgreSQL and MVCC (cont) A transaction sees a consistent view of the database, but it may not see the "current" view of the database. E.g. T1 does a select and then concurrent T2 deletes some of T1's selected tuples This is not a problem unless the transactions communicate outside the database system. For applications that require that every transaction accesses the current consistent version of the data, explicit locking must be used. What are the problems of concurrent transactions?If concurrency control is not maintained, three serious problems may be caused by concurrent transaction execution: lost updates, uncommitted data, and inconsistent retrievals.
What are the potential problems when DBMS executes multiple transactions concurrently?Lost update problem, dirty read problem , unrepeatable read problem and phantom problems are the potential problem that might occur when a DBMS executes multiple transactions concurrently.
When two concurrent transactions update the same data this may lead to the problem called?The lost update problem occurs when 2 concurrent transactions try to read and update the same data. Let's understand this with the help of an example.
|