<< previous page   --   table of contents   --   next page >>
| | | | | | | |
  • Return to Table of Contents
  • Table of Contents

    1. General Information
    2. MySQL Installation
    3. Tutorial Introduction
    4. Database Administration
    5. MySQL Optimisation
    6. MySQL Language Reference
    7. MySQL Table Types
    8. MySQL APIs
    9. Extending MySQL

    520 MySQL Technical Reference for Version 4.0.3 7.5.8.2  Locking Reads A  consistent  read  is  not  convenient  in  some  circumstances.   Suppose  you  want  to  add  a new row into your table CHILD, and make sure that the child already has a parent in table PARENT. Suppose you use a consistent read to read the table  PARENT  and indeed see the parent of the child in the table.  Can you now safely add the child row to table CHILD?  No, because it may happen that meanwhile some other user has deleted the parent row from the table PARENT, and you are not aware of that. The solution is to perform the SELECT in a locking mode, LOCK IN SHARE MODE. SELECT * FROM PARENT WHERE NAME = 'Jones' LOCK IN SHARE MODE; Performing a read in share mode means that we read the latest available data, and set a shared mode lock on the rows we read.   If the latest data belongs to a yet uncommitted transaction of another user, we will wait until that transaction commits.  A shared mode lock prevents others from updating or deleting the row we have read.  After we see that the above query returns the parent  'Jones', we can safely add his child to table  CHILD, and commit our transaction.  This example shows how to implement referential integrity in your application code. Let us look at another example:  we have an integer counter eld in a table CHILD_CODES which we use to assign a unique identi er to each child we add to table CHILD.  Obviously, using a consistent read or a shared mode read to read the present value of the counter is not a good idea, since then two users of the database may see the same value for the counter, and we will get a duplicate key error when we add the two children with the same identi er to the table. In  this  case  there  are  two  good  ways  to  implement  the  reading  and  incrementing  of  the counter:  (1) update the counter rst by incrementing it by 1 and only after that read it, or (2) read the counter rst with a lock mode FOR UPDATE, and increment after that: SELECT COUNTER_FIELD FROM CHILD_CODES FOR UPDATE; UPDATE CHILD_CODES SET COUNTER_FIELD = COUNTER_FIELD + 1; A SELECT ... FOR UPDATE will read the latest available data setting exclusive locks on each row it reads.  Thus it sets the same locks a searched SQL UPDATE would set on the rows. 7.5.8.3  Next-key Locking:  Avoiding the Phantom Problem In row level locking InnoDB uses an algorithm called next-key locking. InnoDB does the row level locking so that when it searches or scans an index of a table, it sets shared or exclusive locks on the index records in encounters.  Thus the row level locks are more precisely called index record locks. The locks InnoDB sets on index records also a ect the 'gap' before that index record.  If a user has a shared or exclusive lock on record R in an index, then another user cannot insert a new index record immediately before R in the index order.  This locking of gaps is done to prevent the so-called phantom problem.  Suppose I want to read and lock all children with identi er bigger than 100 from table CHILD, and update some eld in the selected rows.
     

    Customer Support CentreMySQL Reference Manual

    Web Hosting Services
    UNIX WEB HOSTING
    MERCHANT ACCOUNTS
    DEDICATED SERVERS
    E-COMMERCE HOSTING
    SUPPORT & FAQ's
    TERMS OF USE
    Domain Services
    DOMAIN
    REGISTRATION
    MANAGE
    YOUR ACCOUNT
    SUPPORT & FAQ's
    TERMS OF USE
    Corporate Info
    ABOUT US
    OUR NETWORK
    CONTACT US
    SITE MAP
    Copyright © 2002 Dyntex Group, Inc. All Rights Reserved
  • Return to Table of Contents
  • Back to top

  • Web Hosting: Manuals & FAQ's

    1. Unix-Based Web Hosting
    2. Unix Dedicated Servers
    3. Windows Dedicated Servers
    4. CuteFTP User’s Guide
    5. CuteHTML User’s Guide
    6. WS_FTP Pro User's Guide
    7. Miva Order User's Guide
    8. Miva Merchant User's Guide