Locking unit 1 topic 3


Published on

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Locking unit 1 topic 3

  1. 1. Locking fundamentals <ul><li>Fundamental tool of concurrency control </li></ul><ul><li>Obtain lock before accessing an item </li></ul><ul><li>Wait if a conflicting lock is held </li></ul><ul><ul><li>Shared lock: conflicts with exclusive locks </li></ul></ul><ul><ul><li>Exclusive lock: conflicts with all other kinds of locks </li></ul></ul><ul><li>Concurrency control manager maintains the lock table(which type of lock,on which table,usernm) </li></ul>
  2. 2. Concurrency control techq <ul><li>Concurrency control and locking is the mechanism used by DBMSs for the sharing of data. </li></ul><ul><li>Atomicity, consistency, and isolation are achieved through concurrency control and locking. </li></ul><ul><li>When many people may be reading the same data item at the same time, it is usually necessary to ensure that only one application at a time can change a data item. Locking is a way to do this. </li></ul><ul><li>Because of locking, all changes to a particular data item will be made in the correct order in a transaction.  </li></ul><ul><li>Different types of locks : 1.page locking 2. class/table locking 3. instance/row locking </li></ul><ul><li>Page locking In this, all the data on a specific page are locked. A page is a common unit of storage in computer systems and is used by all types of DBMSs. </li></ul><ul><li>Class or table locking means that all instances of either a class or table are locked,   It represents all instances of a class, regardless of the page where they are stored. </li></ul><ul><li>Instance /row locking locks a single relational tuple in an RDBMS or a single object in an ODBMS </li></ul>
  3. 4. Locking granularity
  4. 5. Locking concept <ul><li>For any db , if more than one user, deals with locking conflicts when two or more users try to change the same row in the database. </li></ul><ul><li>The most common way in which access to items is controlled is by “locks.” </li></ul><ul><li>Lock manager is the part of a DBMS that records, for each item I, whether one or more transactions are reading or writing any part of I. </li></ul><ul><li>If so, the manager will prohibit another transaction from gaining access to I, provided the type of access (read or write) could cause a conflict, such as the duplicate selling of an airline seat. </li></ul>
  5. 6. Understanding locks and transactions <ul><li>Locks prevent multiple users from changing the same data at the same time. </li></ul><ul><li>if one or more rows in a table can be changed, the user executing the DML statement must obtain a lock on the row or rows </li></ul><ul><li>a lock gives the user exclusive control over the data until the user has committed or rolled back the transaction that is changing the data. </li></ul><ul><li>In Oracle 10g, a transaction can lock one row, multiple rows, or an entire table. </li></ul><ul><li>One can manually lock rows & Oracle automatically locks the rows needed at the lowest possible level to ensure data integrity </li></ul><ul><li>Hence conflicts with other transactions that may need to access other rows in the table are minimized </li></ul>
  6. 7. LOCK TABLE statement <ul><li>Allows a user to explicitly acquire a shared or exclusive table lock on the specified table. The table lock lasts until the end of the current transaction. </li></ul><ul><li>Explicitly locking a table is useful for:- </li></ul><ul><ul><li>avoiding the overhead of multiple row locks on a table (in other words, user-initiated lock escalation) </li></ul></ul><ul><ul><li>avoiding deadlocks </li></ul></ul><ul><ul><li>You cannot lock system tables with this statement. </li></ul></ul><ul><li>Syntax </li></ul><ul><li>LOCK TABLE table-Name IN { SHARE | EXCLUSIVE } MODE </li></ul><ul><li>  Once a table is locked in either mode, a transaction does not acquire any subsequent row-level locks on a table. For example, if a transaction locks the table in share mode in order to read data, a particular statement might need to lock a particular row in exclusive mode in order to update the row. </li></ul>
  7. 8. Packaged Applications and Locking (ex. of locking) <ul><li>The HR department recently purchased a benefits management package that interfaced well with our existing employee management tables; however, once they started using the application, other users that accessed the employee tables started complaining of severe slowdowns in updates to the employee information. </li></ul><ul><li>Reviewing the CPU and I/O usage of the instance did not reveal any problems; it wasn’t until I looked at the locking information that I(author) noticed a table lock on the employees table whenever the benefits management features were being used! </li></ul><ul><li>The benefits management application was written to work on a number of database platforms, and the least capable of those platforms did not support row locking. </li></ul><ul><li>As a result, no one could make changes to the employees table whenever an employee’s benefits were being changed, and everyone had to wait for the benefits changes to complete. </li></ul>
  8. 9. example <ul><li>If multiple users require a lock on a row or rows in a table, the first user to request the lock obtains it, and the remaining users are enqueued using a first-in, first-out (FIFO) method. At a SQLprompt, a DML statement (INSERT, UPDATE, DELETE, or MERGE) that is waiting for a lock on a resource appears to hang, unless the NOWAIT keyword is used in a LOCK statement. </li></ul>
  9. 10. Locking in oracle <ul><li>You can explicitly obtain locks on individual rows by using the SELECT … FOR UPDATE statement, as you can see in the following example: </li></ul><ul><li>  SQL> select * from hr.employees  where manager_id = 100  for update; </li></ul><ul><li>  This query not only shows the rows that satisfy the query conditions, it also locks the selected rows and prevents other transactions from locking or updating these rows until a COMMIT or a ROLLBACK occurs. </li></ul><ul><li>NOWAIT Mode Using NOWAIT in a LOCK TABLE statement returns control to the user immediately if any locks already exist on the requested resource, as you can see in the following example: </li></ul><ul><li>SQL> lock table hr.employees in share row exclusive mode nowait;  </li></ul><ul><li>lock table hr.employees  </li></ul><ul><li>ERROR at line 1:  ORA-00054: resource busy and acquire with NOWAIT specified  </li></ul><ul><li>This is especially useful in a PL/SQL application if an alternate execution path can be followed if the requested resource is not yet available. NOWAIT can also be used in the SELECT … FOR UPDATE statement. </li></ul>
  10. 11. Locks <ul><li>small subset of the items may have locks on them at any one time, the lock manager can store the current locks in a lock table which consists of records </li></ul><ul><li>(<item>,<lock type>,<transaction> </li></ul><ul><li>The meaning of record (I,L,T) is that transaction T has a lock of type L on item I. </li></ul>
  11. 12. Example of locks <ul><li>Lets consider two transaction T1 and T2. Each accesses an item A, which we assume has an integer value, and adds one to A. </li></ul><ul><li>Read A;A:=A+1;Write A; </li></ul><ul><li>----------------------------------------------------------- </li></ul><ul><li>T1: Read A A:=A+1 Write A </li></ul><ul><li>T2: Read A A:=A+1 Write A </li></ul><ul><li>----------------------------------------------------------- </li></ul>
  12. 13. Example of locks contd … <ul><li>The most common solution to this problem is to provide a lock on A. </li></ul><ul><li>Before reading A, a transaction T must lock A, which prevents another transaction from accessing A until T is finished with A. </li></ul><ul><li>Furthermore, the need for T to set a lock on A prevents T from accessing A if some other transaction is already using A. </li></ul><ul><li>T must wait until the other transaction unlocks A, which it should do only after finishing with A. </li></ul>
  13. 14. Concurrent access with transaction
  14. 15. Deadlock(mutual waiting)
  15. 16. deadlocks <ul><li>A deadlocks involves a chain of transactions that are cyclically waiting for each other to release a lock. </li></ul><ul><li>The DBMS detects deadlock with a transaction dependency graph. It resolves the impasse by sacrificing one of the transactions in cycle . </li></ul>
  16. 17. Deadlocks and transaction dependency graph
  17. 18. Detecting and Resolving Lock Conflicts <ul><li>Although locks are a common and sometimes unavoidable occurrence in many databases, they are usually resolved by waiting in the queue. </li></ul><ul><li>In some cases, you may need to resolve the lock problem manually (for example, if a user makes an update at 4: 59 P.M. and does not perform a COMMIT before leaving for the day). </li></ul>
  18. 19. Detecting Lock Conflicts <ul><li>Detecting locks in Oracle 10g to see who is locking what resource. </li></ul><ul><li>Example: Execute the following statement: </li></ul><ul><li>SQL> lock table hr.employees, hr.departments in exclusive mode;  Table(s) Locked. </li></ul><ul><li>SCOTT has an EXCLUSIVE lock on both the EMPLOYEES and DEPARTMENTS table. </li></ul><ul><li>You can drill down on the locked object by clicking one of the links in the Object Name column; similarly, you can review other information about SCOTT’s session by clicking one of the links in the Session ID column. </li></ul>
  19. 20. Deadlock in oracle <ul><li>A deadlock is a special type of lock conflict in which two or more users are waiting for a resource locked by the other users. </li></ul><ul><li>As a result, neither transaction can complete without some kind of intervention:- </li></ul><ul><li>the session that first detects a deadlock rolls back the statement waiting on the resource with the error message ORA-00060: Deadlock detected while waiting for resource. </li></ul><ul><li>After the error message is issued at 11:45, the second UPDATE for Session 1 does not succeed; </li></ul><ul><li>however, the second UPDATE for Session 2 completes, and the user in Session 2 can now submit another DML statement or issue a COMMIT or ROLLBACK. </li></ul><ul><li>The user in Session 1 will have to re-issue the second UPDATE. </li></ul><ul><li>  </li></ul>
  20. 21. Choosing a locking strategy <ul><li>Lock table manually overrides default locking </li></ul><ul><li>When a lock table issued on a view,the underlying tables are locked </li></ul><ul><li>Lock table emp_tab,dept_tab in EXCLUSIVE MODE NOWAIT; </li></ul><ul><li>Lock table emp_tab in ROW SHARE MODE; </li></ul><ul><li>Lock table emp_tab in ROW EXCLUSIVE MODE; </li></ul><ul><li>They offer highest degree of concurrency; you might use them </li></ul><ul><ul><li>Your transaction needs to prevent another transaction from acquiring or exclusive table lock for a table before table is updated in your transaction </li></ul></ul><ul><ul><li>No other transaction can update the table until transaction commits/rolls back </li></ul></ul><ul><ul><li>Your transaction needs to prevent from being altered/dropped before the table can be modified </li></ul></ul><ul><ul><li>To lock with share mode:- Lock table in share mode; </li></ul></ul>
  21. 22. Choosing a locking strategy Practically <ul><li>Lock table dept_tab in share mode; </li></ul><ul><li>Update emp_tab set sal=sal*.1 where deptno in(select deptno from dept where loc=‘mum’); </li></ul><ul><li>Update budget_tab set total=tal*.1 where deptno in(select deptno from dept where loc=‘mum’; </li></ul><ul><li>Commit; </li></ul><ul><li>To lock table in share row exclusive mode :- </li></ul><ul><ul><li>Lock table emp_tab in SHARE ROW EXCLUSIVE MODE; </li></ul></ul><ul><ul><li>SHARE ROW EXCLUSIVE MODE this can be used if transaction level read consistency for specified table and the ability to update the locked table is required </li></ul></ul><ul><ul><li>If other table acquire explicit row locks (using select.. For update) which might make update and insert stmt in locking transaction wait and might cause deadlocks </li></ul></ul><ul><ul><li>In exclusive mode:- lock table <table name) in EXCLUSIVE MODE; </li></ul></ul>
  22. 23. Serial equivalent order on conflict <ul><li>Three ways to ensure a serial-equivalent order on conflicts: </li></ul><ul><ul><li>Option 1 , execute transactions serially. </li></ul></ul><ul><ul><ul><li>“ single shot” transactions </li></ul></ul></ul><ul><ul><li>Option 2 , pessimistic concurrency control : block T until transactions with conflicting operations are done. </li></ul></ul><ul><ul><ul><li>use locks for mutual exclusion </li></ul></ul></ul><ul><ul><ul><li>two-phase locking (2PL) required for strict isolation </li></ul></ul></ul><ul><ul><li>Option 3 , optimistic concurrency control : proceed as if no conflicts will occur, and recover if constraints are violated. </li></ul></ul><ul><ul><ul><li>Repair the damage by rolling back (aborting) one of the conflicting transactions. </li></ul></ul></ul><ul><ul><li>Option 4 , hybrid timestamp ordering using versions. </li></ul></ul>
  23. 24. Pessimistic concurrency control <ul><li>Pessimistic concurrency control uses locking to prevent illegal conflict orderings. </li></ul><ul><ul><ul><li>avoid/reduce expensive rollbacks </li></ul></ul></ul><ul><ul><li>Well-formed : acquire lock before accessing each data item. </li></ul></ul><ul><ul><ul><li>Concurrent transactions T and S race for locks on conflicting data items (say x and y ).... </li></ul></ul></ul><ul><ul><ul><li>Locks are often implicit, e.g., on first access to a data object/page. </li></ul></ul></ul><ul><ul><li>No acquires after release : hold all locks at least until all needed locks have been acquired (2PL). </li></ul></ul><ul><ul><ul><li>growing phase vs. shrinking phase </li></ul></ul></ul><ul><ul><li>Problem : possible deadlock. </li></ul></ul><ul><ul><ul><li>prevention vs. detection and recovery </li></ul></ul></ul>
  24. 25. What is 2PC(Two phase Commit)? <ul><li>Concurrency control in the distributed db environment is important because multistate, multiprocess operations are likely to create data inconsistencies. </li></ul><ul><li>For ex. a TP component of a DDBMS must ensure that all parts of transaction are completed at all sites before a final COMMIT is issued to record the transaction. </li></ul><ul><li>Suppose each transaction operation was committed by each client(distributed processing), but one of them must not commit the transaction’s results </li></ul><ul><li>Such a scenario would create an inconsistent state,because of integrity problems,because uncommitted data can not be committed. </li></ul><ul><li>The solution for the same is given by two phase commit protocol </li></ul>
  25. 26. Two phase commit <ul><li>Distributed db allow transaction to be done at all sites. </li></ul><ul><li>A final commit must not be issued until all sites have committed their parts of transaction. </li></ul><ul><li>Two PC guarantees that if a portion of a transaction operation can not be committed,all changes made at other sites participating in the transaction will be undone to maintain a consistent db state </li></ul>
  26. 27. 2PC <ul><li>Each Distributed processing(dp) site maintains its own transaction log </li></ul><ul><li>The two pc protocol requires that the transaction entry log for each DP be written before the db fragment is actually updated </li></ul><ul><li>The Do-Undo-Redo protocol is used by dp to rollback/roll forward transaction with the help of system’s transaction log entries </li></ul><ul><li>It defines three types of operations :- </li></ul><ul><ul><li>DO performs the operation and records the before and after values in the transaction log </li></ul></ul><ul><ul><li>UNDO reverses an operation ,using the log entries written by DO portion of the sequence </li></ul></ul><ul><ul><li>REDO redoes an operation,using the log entries written by the DO portion of the sequence </li></ul></ul><ul><ul><li>To ensure that DO,UNDO,REDO can survive a system crash while they are being executed,a write ahead log protocol is used. </li></ul></ul><ul><ul><li>It forces log entry to be written to permanent storage before the actual operation takes place. </li></ul></ul>
  27. 28. Two PC <ul><li>This protocol defines the operations between two types of nodes:- co-ordinator and one or more subordinates </li></ul><ul><li>The participating nodes agree on a coordinator. The coordinator role is assigned to the node that initiates the transaction </li></ul><ul><li>Phase 1:Preparation </li></ul><ul><ul><li>The coordinator sends a PREPARE to COMMIT mesg to all subordinates </li></ul></ul><ul><ul><li>The subordinates receive the mesg,write the transaction log,using write ahead protocol and send an acknowledgement(YES/prepared to commit and NO/nor prepared)mesg to coordinator </li></ul></ul><ul><ul><li>The coordinator makes sure that all nodes are ready to commit,or it aborts the action </li></ul></ul><ul><li>If all nodes are prepared to commit,the transaction goes to phase2. If one/more nodes reply NO,the co-ordinator broadcasts an ABORT mesg to all subordinates </li></ul>
  28. 29. TWO PC <ul><li>Phase 2: the Final Commit </li></ul><ul><ul><li>The coordinator broadcasts a COMMIT mesg to all subordinates and waits for the replies </li></ul></ul><ul><ul><li>Each subordinate receives the COMMIT mesg,then updates the db using DO protocol </li></ul></ul><ul><ul><li>The subordinates reply with a COMMITTED or NOT COMMITTED mesg to the coordinator </li></ul></ul><ul><ul><li>If one/more subordinates did not commit, the coordinator sends an ABORT mesg,thereby forcing them to UNDO all changes </li></ul></ul><ul><ul><li>The objective of 2pc is to ensure that all nodes commit their part of the transaction otherwise the transaction is aborted </li></ul></ul><ul><ul><li>If one node fails to commit,the info necessary to recover the db is in the transaction log,the db is recovered with DO-UNDO-REDO protocol </li></ul></ul>
  29. 30. 2PL <ul><li>2PL defines how transactions acquire and relinquish(release) locks. </li></ul><ul><li>Two phase locking guarantees serializability,but it doesn’t provide deadlocks. The two phases are:- </li></ul><ul><ul><li>A growing phase </li></ul></ul><ul><ul><li>A shrinking phase </li></ul></ul><ul><ul><li>A growing phase ,in which a transaction acquires all required locks without unlocking any data. Once all locks have been acquired, the transaction is in its locked point </li></ul></ul><ul><ul><li>A shrinking phase,in which a transaction releases all locks and can not obtain any new lock </li></ul></ul>
  30. 31. Two phase locking Rules <ul><li>Two transactions can not have a conflicting locks </li></ul><ul><li>No unlock operation can precede a lock operation in the same transaction </li></ul><ul><li>No data are affected until all locks are obtained- that is,until the transaction is in its locked point </li></ul><ul><li>Diagram- rob n colonel pg.413 </li></ul><ul><li>The transaction acquires all locks it needs until it reaches its locked point </li></ul><ul><li>Two phase locking increases the transaction processing cost and may not cause additional undesirable effects. One such effect is deadlock. </li></ul>
  31. 32. Why 2PL? <ul><li>If transactions are well-formed, then an arc from T to S in the schedule graph indicates that T beat S to some lock. </li></ul><ul><ul><ul><li>Neither could access the shared item x without holding its lock. </li></ul></ul></ul><ul><ul><ul><li>Read the arc as “ T holds a resource needed by S ”. </li></ul></ul></ul><ul><li>2PL guarantees that the “winning” transaction T holds all its locks at some point during its execution. </li></ul>Thus 2PL guarantees that T “won the race” for all the locks... ...or else a deadlock would have resulted. T: R(A) W(A) R(C) W(C) S: R(A) R(C) A C T S T S
  32. 33. Why 2PL? <ul><li>Consider our two transactions T and S : </li></ul><ul><ul><li>T : transfer $100 from A to C: R(A) W(A) R(C) W(C) </li></ul></ul><ul><ul><li>S : compute total balance for A and C : R(A) R(C) </li></ul></ul><ul><li>Non-two-phased locking might not prevent the illegal schedules. </li></ul>T: R(A) W(A) R(C) W(C) S: R(A) R(C) A T: R(A) W(A) R(C) W(C) S: R(A) R(C) C A C T S T S
  33. 34. More on 2PL(from Ramakrishnan) <ul><li>The strict 2PL protocol allows only conflict serializable schedules </li></ul><ul><li>Two schedules are said to be conflict equivalent if they involve the same set of actions of the same transactions </li></ul><ul><li>Two actions conflict if they operate on the same data object and at least one of them is write </li></ul><ul><li>If two schedules are conflict equivalent ,it is easy to see that they have the same effect on the db </li></ul><ul><li>A schedule is conflict serializable if it is conflict equivalent to some serial schedule. Every conflict serializable schedule is serializable,if we assume set of items in the db does not grow/shrink, values can be modified but items can’t added/deleted </li></ul>
  34. 35. 2PL(from Ramakrishan) <ul><li>Strict 2PL allows only conflict serializable schedules, is seen from following two results: </li></ul><ul><ul><li>A schedule S is conflict serializable if and only if its precedence graph is acyclic </li></ul></ul><ul><ul><li>Strict 2PL ensures that the precedence graph for any schedule that it allows is acyclic </li></ul></ul><ul><ul><li>Variant of strict 2PL called 2PL relaxes 2 nd rule to allow transactions to release locks before the end i.e. before commit or abort action </li></ul></ul><ul><ul><li>2PL 2 nd rule is replaced by following rule :- </li></ul></ul><ul><ul><ul><li>A transaction can not request additional locks once it releases any lock </li></ul></ul></ul><ul><ul><ul><li>hence every transaction has growing phase in which it acquires locks,followed by shrinking phase in which it releases locks </li></ul></ul></ul>
  35. 36. Concurrency control by time stamps <ul><li>Pg415 colonel </li></ul><ul><li>The time stamping approach to scheduling concurrent transactions assigns a global,unique time stamp to each transaction </li></ul><ul><li>Time stamp value produces an explicit order in which transactions are submitted to DBMS. </li></ul><ul><li>The stamps must have two properties: uniqueness & monotonicity </li></ul><ul><li>Uniqueness ensures that no equal time stamp values can exist </li></ul><ul><li>Monotonicity ensures that the time stamp values always increase (Obvious!) </li></ul><ul><li>The DBMS executes conflicting operations in the time stamp order,thereby ensuring serializability of transactions </li></ul><ul><li>If two transactions conflict,one is stopped,rolled back,rescheduled & assigned a new time stamp value </li></ul><ul><li>Disadvantage is:- each value stored in the database requires two additional time stamp fields. One for the last time the field was read and one for last update. Time stamping increases memory needs and the db’s processing overhead </li></ul><ul><li>It needs a lot of system resources because many transactions may have to be stopped ,rescheduled and restamped </li></ul>
  36. 37. Time stamp based concurrency control <ul><li>In lock based concurrency control,conflicting actions of different transactions are ordered by the order in which locks are obtained </li></ul><ul><li>In optimistic concurrency control,a timestamp ordering is imposed on transactions and validations checks that all conflicting actions occurred in the same order </li></ul><ul><li>The optimistic approach does not require locking or time stamping techq. using an optimistic approach,each transaction moves thru two or three phases:-they are read, validation and write </li></ul><ul><li>Read phase: reads the db,executes the needed computations,and makes the updates to private copy of db values. All update operations of the transaction are recorded in a temp.update file,which is not accessed by remaining transactions </li></ul><ul><li>Validation phase :the transaction is validated to ensure that the changes made will not affect the integrity and consistency of the db.If validation test is +ve, the transaction goes to write phase else if it is –ve,then the transaction is restarted and changes are discarded </li></ul><ul><li>During write phase: the changes are permanently applied to the database </li></ul>
  37. 38. Concurrency control without locking <ul><li>Types of concurrency control without locking are: </li></ul><ul><ul><li>Optimistic </li></ul></ul><ul><ul><li>Pessimistic </li></ul></ul><ul><ul><li>The optimistic approach is acceptable for most read or query db systems that require few update transactions </li></ul></ul><ul><ul><li>Deadlocks is difficult to avoid,therefore it is necessary to employ db recovery techq.to restore the db to a consistent state </li></ul></ul><ul><ul><li>The management of deadlocks in terms of prevention and detection constitutes an imp.db function </li></ul></ul><ul><ul><li>dealing with deadlocks (Detection) :- in practice,db systems periodically check for deadlocks. When a transaction Ti is suspended because a lock that it requests can not be granted,it must wait until all transactions Tj that currently hold conflicting locks release them. </li></ul></ul><ul><ul><li>The locks manager maintains a structure called as waits-for graph to detect deadlock cycles </li></ul></ul><ul><ul><li>The nodes correspond to active transactions, and there is an arc from Ti to Tj iff Ti is waiting for Tj to release a lock </li></ul></ul><ul><ul><li>Alternative to maintaining a waits-for graph is to identify deadlocks through a timeout mechanism:- if a transaction has been waiting too long for a lock,we assume it is in a deadlock cycle and abort it </li></ul></ul>
  38. 39. Optimistic and pessimistic approach <ul><li>Locking protocols take a Pessimistic approach to conflicts between transactions and use either a transaction abort or blocking to resolve conflicts </li></ul><ul><li>Optimistic approach’s basic idea is that most transactions do not conflict with other transactions and the idea is to be permissive as possible in allowing transactions to execute. </li></ul><ul><li>Transactions proceed in 3 phases:- </li></ul><ul><ul><li>Read – the transaction executes , reading values from db and writing to a private workspace </li></ul></ul><ul><ul><li>Validation- if the transaction decides it wants to commit,the dbms checks with other transactions whether it has been conflicted. If it has, the transaction is aborted, the private workspace is cleared and it is restarted </li></ul></ul><ul><ul><li>Write- if validation determines that there no possible conflicts,the changes to data objects made by the transaction in its private workspace are copied into the database. </li></ul></ul>
  39. 40. Optimistic contd…(validation based protocol) <ul><li>Each transaction Ti is assigned a timestamp TS(Ti) at the beginning of validation phase,the validation criteria checks whether the timestamp ordering of transaction is equivalent serial order </li></ul><ul><li>Known as optimistic concurrency control since transaction executes fully in the hope that all will go well during validation </li></ul><ul><li>For every pair of transaction Ti and Tj such that TS(Ti)<TS(Tj), one of the following validation must hold:- </li></ul><ul><ul><ul><li>Ti completes(all 3phases) before Tj begins </li></ul></ul></ul><ul><ul><ul><li>Ti completes before Tj starts its Write phase, and Ti does not write any db object read by Tj </li></ul></ul></ul><ul><ul><ul><li>Ti completes its Read phase before Tj completes its Read phase, and Ti doesn’t write any db object that is either read or written by Tj </li></ul></ul></ul><ul><ul><ul><li>To validate Tj,we must check to see that out of these coonditions holds with respect to each committed transaction Ti such that TS(Ti)<TS(Tj). Each of these conditions ensure that Tj’s modifications are not visible to Ti. </li></ul></ul></ul>
  40. 41. Deadlock prevention <ul><li>If there is a high level of conflict of locks, prevention based schemes could perform better </li></ul><ul><li>We can prevent them by giving each transaction a priority and ensuring lower priority transactions are not allowed to wait for higher priority transactions(vice a versa) </li></ul><ul><li>One way to assign priorities is to give each transaction a timestamp when it starts up. </li></ul><ul><li>The lower the timestamp, higher is the transaction’s priority i.e. oldest transaction has the highest priority(Bakery algo in OS) </li></ul><ul><li>If transaction Ti requests a lock and transaction Tj holds a conflicting lock,the lock manager can use one of below policies:- </li></ul><ul><ul><li>Wait-die : if Ti has higher priority,it is allowed to wait,otherwise,it is aborted </li></ul></ul><ul><ul><li>Wound-wait: if Ti has higher priority, abort Tj ;otherwise , Ti waits </li></ul></ul><ul><ul><li>In Wait-die scheme ,lower priority transactions can never wait for higher priority trnasactions </li></ul></ul><ul><ul><li>In wound-wait scheme, higher priority transactions never wait for lower priority transactions. In either case, no deadlocks cycle develops </li></ul></ul>
  41. 42. Deadlock Recovery <ul><li>When deadlock is detected : </li></ul><ul><ul><li>Some transaction will have to rolled back (made a victim) to break deadlock . Select that transaction as victim that will incur minimum cost. </li></ul></ul><ul><ul><li>Rollback -- determine how far to roll back transaction </li></ul></ul><ul><ul><ul><li>Total rollback : Abort the transaction and then restart it. </li></ul></ul></ul><ul><ul><ul><li>More effective to roll back transaction only as far as necessary to break deadlock. </li></ul></ul></ul><ul><ul><li>Starvation happens if same transaction is always chosen as victim. Include the number of rollbacks in the cost factor to avoid starvation </li></ul></ul>
  42. 43. Conservative 2PL <ul><li>A variant of 2PL,called conservative 2PL,which also prevents deadlock. under this type, a transaction obtains all the locks it will never need when it begins, or blocks waiting for these locks to become available. </li></ul><ul><li>This scheme ensures that there will be no deadlocks and a transaction that already holds some locks will not blocks waiting for other locks </li></ul><ul><li>If lock contention(conflict) is heavy,conservative 2PL can reduce the time that locks are held on average, because transactions that holds locks are never blocked </li></ul><ul><li>If lock contention is low, locks are held longer under Conservative 2PL. </li></ul><ul><li>From pract’s point of view,it is hard to know exactly what locks are needed ahead of time and it may set more no.of locks than necessary </li></ul><ul><li>It also has higher overhead for setting locks because a transaction has to release all locks and try to obtain them all over again if it needs </li></ul><ul><li>Hence this techq.is not used in practice </li></ul>
  43. 44. Thomas Write Rule <ul><li>When T i attempts to write data item Q , if TS( T i ) < W-timestamp( Q ), then T i is attempting to write an obsolete value of { Q }. TS-Time stamp </li></ul><ul><li>if TS(T)<W-time stamp(O),the current write action has been made obsolete(out of date) by the most recent write of O,which follows the current write according to time stamp ordering </li></ul><ul><li>T’s write action as if it had occurred immediately before the most recent write of O and was never read by anyone </li></ul><ul><li>Hence, rather than rolling back T i as the timestamp ordering protocol would have done, this { write } operation can be ignored. </li></ul><ul><li>Otherwise this protocol is the same as the timestamp ordering protocol. </li></ul><ul><li>Thomas' Write Rule allows greater potential concurrency </li></ul>