Beruflich Dokumente
Kultur Dokumente
How is concurrency control actually implemented in a DBMS? Locking Timestamp Multiversion Phantoms Index Locking Rollback Demo
Dr. Philip Cannata Data Management 1
Lock-Based Protocols
A lock is a mechanism to control concurrent access to a data item Data items can be locked in two modes : 1. shared (S) mode. Used if a transaction only wants to read a data item. Granted via an lock-S instruction. 2. exclusive (X) mode. Used if a transaction wants to read as well as write a data item. Granted via an lock-X instruction Lock requests are made to the concurrency-control manager. Transaction can proceed only after request is granted.
Data Management
A transaction may be granted a lock on an item if the requested lock is compatible with locks already held on the item by other transactions Any number of transactions can hold shared locks on an item, but if any transaction holds an exclusive on the item no other transaction may hold any lock on the item. If a lock cannot be granted, the requesting transaction is made to wait until all incompatible locks held by other transactions have been released. The lock is then granted.
Dr. Philip Cannata Data Management 3
Data Management
Data Management
Neither T3 nor T4 can make progress executing lock-S(B) causes T4 to wait for T3 to release its lock on B, while executing lock-X(A) causes T3 to wait for T4 to release its lock on A. Such a situation is called a deadlock. To handle a deadlock one of T3 or T4 must be rolled back and its locks released.
Dr. Philip Cannata Data Management 8
Deadlock Detection
Deadlocks can be described as a wait-for graph, which consists of a pair G = (V,E), V is a set of vertices (all the transactions in the system) E is a set of edges; each element is an ordered pair Ti Tj. If Ti Tj is in E, then there is a directed edge from Ti to Tj, implying that Ti is waiting for Tj to release a data item. When Ti requests a data item currently being held by Tj, then the edge Ti Tj is inserted in the wait-for graph. This edge is removed only when Tj is no longer holding a data item needed by Ti. The system is in a deadlock state if and only if the wait-for graph has a cycle. Must invoke a deadlock-detection algorithm periodically to look for cycles.
Dr. Philip Cannata Data Management 9
Data Management
10
Deadlock Recovery
When deadlock is detected : Some transaction will have to rolled back (made a victim) to break deadlock. Select that transaction as victim that will incur minimum cost. Rollback -- determine how far to roll back transaction Total rollback: Abort the transaction and then restart it. More effective to roll back transaction only as far as necessary to break deadlock. Starvation happens if same transaction is always chosen as victim. Include the number of rollbacks in the cost factor to avoid starvation
Dr. Philip Cannata Data Management 11
User2-T5.sql
commit ; set transaction isolation level serializable ; update emp set sal = sal + 1 where empno = 7934 ; update emp set sal = sal + 1 where empno = 7839 ; select empno, ename, sal from emp where empno = 7934 ; select empno, ename, sal from emp where empno = 7839 ; pause "Press Enter to continue" commit ;
Data Management
12
Data Management
13
Data Management
14
Data Management
15
Data Management
16
Data Management
17
Free Space
PCTINCREASE causes the size of each successive extent to increase geometrically by the pctincrease value Segment a set of extents within a single tablespace
FREELIST list of blocks below PCTUSED in a segment FREELISTS number of freelists in a segment Tablespace logical collection of a set of database or index files 18 BUFFER_POOL KEEP, RECYCLE or DEFAULT
Timestamp-Based Protocols
Each transaction is issued a timestamp when it enters the system. If an old transaction Ti has time-stamp TS(Ti), a new transaction Tj is assigned time-stamp TS(Tj) such that TS(Ti) < TS(Tj). The protocol manages concurrent execution such that the timestamps determine the serializability order. In order to assure such behavior, the protocol maintains for each data item Q two timestamp values: W-timestamp(Q) is the largest time-stamp of any transaction that executed write(Q) successfully. R-timestamp(Q) is the largest time-stamp of any transaction that executed read(Q) successfully.
Dr. Philip Cannata Data Management 19
1.
2.
Data Management
20
2.
3.
Data Management
21
read2(Y)
write4(Y) write5(Z) read7(Y) abort write9(Z) abort
read6(Z)
R W R W R W 1 2 4 6 5 t5 t2 t3 t5 t3
read8(X)
write(Y) write(Z)
Now this needs to be rolled back
Data Management
22
Thus, there will be no cycles in the precedence graph Timestamp protocol ensures freedom from deadlock as no transaction ever waits. But the schedule may not be cascade-free, and may not even be recoverable.
Dr. Philip Cannata Data Management 23
Multiversion Schemes
Multiversion schemes keep old versions of data item to increase concurrency. Multiversion Timestamp Ordering Multiversion Two-Phase Locking Each successful write results in the creation of a new version of the data item written. Use timestamps to label versions. When a read(Q) operation is issued, select an appropriate version of Q based on the timestamp of the transaction, and return the value of the selected version. reads never have to wait as an appropriate version is returned immediately.
Dr. Philip Cannata Data Management 25
Data Management
26
1. 2.
T2t2
read2(Y)->Y0
T3t3
T4t4
T5t5
read1(X)->X0
Version 0 C X Y Z X0 Y0 Z0
Dr. Philip Cannata
Version 1 C Y1 Z1
6:t5
write(Y) write(Z)
Still have cascading rollback
R
1:t5 2:t2
W
4:t3 5:t3
Data Management
28
T2
read2(Y)->Y0
T3
T4
T5
read1(X)->X0
Locked by
Version 0 C SCN
Version 1 C SCN
abort
No cascading rollback
write(Y) write(Z)
X
Y Z
4:T3 5:T3
X0
Y0 Z0
0
0 0 Y1 Z1
Data Management
31
User2-T6.sql
commit ; set transaction isolation level serializable ; update emp set sal = sal + 1 select empno, ename, sal from emp where empno = 7369 ; where empno = 7369 ; pause "Press Enter to continue" pause "Press Enter to continue" update emp set sal = sal + 1 select empno, ename, sal from emp where empno = 7369 ; where empno = 7369 ; commit ; commit ;
Data Management
32
Step 1 to here
Step 2 to here
Step 3 to here
Step 4 to here
Data Management
33
Step 1 to here
Step 2 to here
Step 4 to here
Step 3 to here
Data Management
34
Step 2 to here
Step 4 to here
Step 1 to here
Step 3 to here
Data Management
35
User2-T7.sql
commit ; set transaction isolation level serializable ; update emp set sal = sal + 1 where empno = 7369 ; pause "Press Enter to continue" update emp set sal = sal + 1 where empno = 7369 ; commit ;
Data Management
36
Step 2 to here
Step 1 to here
Data Management
37
User2-T8.sql
update emp set sal = 5000 where empno = 7844 ; commit ;
Data Management
40
Step 1 to here
Data Management
42
User2-T9.sql
update emp set sal = 5000 where empno = 7844 ; commit ;
Data Management
43
Oracle
Data Management
48
Dirty Write The possibility that two transactions might both perform an update on the same row before either transaction terminates
Data Management
49
Application designers and developers should choose an isolation level that is appropriate to the specific application and workload, and may choose different isolation levels for different transactions. The choice should be based on performance and consistency needs, and consideration of application coding requirements. For environments with many concurrent users rapidly submitting transactions, designers must assess transaction performance requirements in terms of the expected transaction arrival rate and response time demands, and choose an isolation level that provides the required degree of consistency while satisfying performance expectations. Frequently, for high performance environments, the choice of isolation levels involves making a trade-off between consistency and concurrency (transaction throughput). Both Oracle isolation modes provide high levels of consistency and concurrency (and performance) through the combination of row-level locking and Oracle's multi-version concurrency control system. Because readers and writers don't block one another in Oracle, while queries still see consistent data, both READ COMMITTED and SERIALIZABLE isolation provide a high level of concurrency for high performance, without the need for reading uncommitted ("dirty") data.
Dr. Philip Cannata Data Management 50
READ COMMITTED isolation can provide considerably more concurrency with a somewhat increased risk of inconsistent results (due to phantoms and non-repeatable reads) for some transactions. The SERIALIZABLE isolation level provides somewhat more consistency by protecting against phantoms and non-repeatable reads, and may be important where a read/write transaction executes a query more than once. However, SERIALIZABLE mode requires applications to check for the "can't serialize access" error, and can significantly reduce throughput in an environment with many concurrent transactions accessing the same data for update. Application logic that checks database consistency must take into account the fact reads don't block writes in either mode.
Data Management
51
When a transaction runs in serializable mode, any attempt to change data that was changed by another transaction since the beginning of the serializable transaction results in the following error:
ORA-08177: Can't serialize access for this transaction. When you get an ORA-08177 error, the appropriate action is to roll back the current transaction, and re-execute it. After a rollback, the transaction acquires a new transaction snapshot, and the DML operation is likely to succeed. Because a rollback and repeat of the transaction is required, it is good development practice to put DML statements that might conflict with other concurrent transactions towards the beginning of your transaction, whenever possible.
Data Management
52
User1-Rollback.sql
Rollback Demo
commit ; set transaction isolation level serializable ; select empno, ename, sal from emp where empno = 7839; update emp set sal = sal + 10 where empno = 7839 ; pause "Press Enter to continue" ; select empno, ename, sal from emp where empno = 7839; commit ;
set serverout on begin commit ; set transaction isolation level serializable ; update emp set sal = sal + 1 where empno = 7839 ; commit ; dbms_output.put_line('*** Commit done.') ; exception when others then rollback ; dbms_output.put_line('*** Rollback done.') ; end ; /
User2-Rollback.sql
Data Management
53
Step 1 to here
Step 2 to here
Step 3 to here
Data Management
54
Data Management
55
Concurrency Control
Write a couple oracle concurrency control demos of your own where you show insert and delete phantoms.
Data Management
56