Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Locks with updt nowait


Published on

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

  • Be the first to like this

Locks with updt nowait

  1. 1. Locks <ul><li>Users manipulate oracle table data via sql/pl-sql stmt </li></ul><ul><li>An oracle transaction can be made up of single sql stmt or several sentences </li></ul><ul><li>The oracle table is global resource , as it is shared by several users </li></ul><ul><li>There is a need to ensure that integrity of data should be maintained each time when data is accessed </li></ul><ul><li>The simultaneous access to table data has to be granted without causing damage to the data </li></ul>
  2. 2. Concurrency control <ul><li>This techq employed by Oracle engine to protect table data when several people are accessing it is called as concurrency control </li></ul><ul><li>Oracle uses a method of locking to implement concurrency control when multiple users access a table for manipulation of data at the same time </li></ul>
  3. 3. locking <ul><li>Types of locks </li></ul><ul><ul><li>Type of lock to be applied </li></ul></ul><ul><ul><li>Level of lock to be applied </li></ul></ul><ul><ul><li>Read Operation :- select stmt </li></ul></ul><ul><ul><li>Write Operation:- Insert,Update,Delete stmt </li></ul></ul><ul><ul><li>Since read doesn’t make any changes in a table and are meant only viewing purpose </li></ul></ul><ul><ul><li>Read can be performed on the table without any danger to the table’s data </li></ul></ul><ul><ul><li>Hence oracle engine places shared lock on a table when its data is being viewed </li></ul></ul>
  4. 4. locking <ul><li>Write operations cause a change in the table data i.e. any insert,update or delete stmt affects table data directly and hence write operation can affect the data integrity </li></ul><ul><li>Simultaneous write operation will cause loss of data consistency in the table </li></ul><ul><li>Hence oracle engine places exclusive lock on a table on specific table’s resources when data is being written to a table </li></ul>
  5. 5. Rules of locking <ul><li>Data being changed can not be READ </li></ul><ul><li>Writers wait for other writers , if they attempt to update the same rows at the same time </li></ul><ul><li>Two types of locks:- </li></ul><ul><ul><li>Shared locks: shared locks are placed on resources whenever a READ operation(select) is performed </li></ul></ul><ul><ul><li>Multiple shared locks can be simultaneously set on a resource </li></ul></ul><ul><ul><li>Exclusive locks: they are placed on resources at a time i.e. the first user who acquires an exclusive lock will continue to have the sole ownership of the resource and no other user can acquire an exclusive lock on that resource </li></ul></ul>
  6. 6. Levels of locks <ul><li>Oracle provides locking </li></ul><ul><ul><li>Row level </li></ul></ul><ul><ul><li>Page level </li></ul></ul><ul><ul><li>Table level </li></ul></ul><ul><ul><li>The oracle engine decides on the level of lock to be used by the presence or absence of a where condition </li></ul></ul><ul><ul><li>If the where clause evaluates to one row in the table,a row level lock is used </li></ul></ul><ul><ul><li>if the where clause evaluates to a set of data, a page level lock is used </li></ul></ul><ul><ul><li>If there is no where clause, a table level lock(query accesses entire table) is used </li></ul></ul>
  7. 7. Implicit lock & explicit lock <ul><li>If client A fires an update cmd on a record in prod_master then oracle will implicitly lock the record so that no further data manipulation can be done by any other user till the lock is released. It can be done by commit/rollback </li></ul><ul><li>In the mean time, client B tries to view the same record, the oracle displays old set of data , this leads to wrong information </li></ul><ul><li>Client A must explicitly lock the record , no other user can access the record even for viewing purpose will client A’s transaction is completed. This is explicit locking </li></ul>
  8. 8. Explicit locking <ul><li>Users can make explicit locking on which they have been granted table privileges </li></ul><ul><li>Rows/tables can be explicitly locked by using either select .. for update …stmt or lock table stmt </li></ul><ul><li>The techq of lock taken or its resources by a user is called explicit locking </li></ul>
  9. 9. Locking
  10. 15. Why and how Choosing a Locking Strategy <ul><li>A transaction explicitly acquires the specified table locks when a LOCK TABLE statement is executed. </li></ul><ul><li>A LOCK TABLE statement manually overrides default locking. </li></ul><ul><li>When a LOCK TABLE statement is issued on a view, the underlying base tables are locked. </li></ul><ul><li>The following statement acquires exclusive table locks for the EMP_TAB and DEPT_TAB tables on behalf of the containing transaction: </li></ul><ul><li>LOCK TABLE Emp_tab, Dept_tab IN EXCLUSIVE MODE NOWAIT; </li></ul><ul><li>You can specify several tables or views to lock in the same mode; however, only a single lock mode can be specified for each LOCK TABLE statement. </li></ul>
  11. 16. Locking.. <ul><li>When a table is locked, all rows of the table are locked. No other user can modify the table. </li></ul><ul><li>You can also indicate if you do or do not want to wait to acquire the lock. If you specify the NOWAIT option, then you only acquire the table lock if it is immediately available. </li></ul>
  12. 17. Locking with Nowait If NOWAIT is omitted, then the transaction does not proceed until the requested table lock is acquired. If the wait for a table lock is excessive, then you might want to cancel the lock operation and retry at a later time; you can code this logic into your applications.
  13. 18. Using Pragma… <ul><li>You can use the pragma EXCEPTION_INIT to associate exception names with other Oracle error codes that you can anticipate. To handle unexpected Oracle errors, you can use the OTHERS handler. Within this handler, you can call the functions SQLCODE and SQLERRM to return the Oracle error code and message text. Once you know the error code, you can use it with pragma EXCEPTION_INIT and write a handler specifically for that error. </li></ul>