List:General Discussion« Previous MessageNext Message »
From:Thimble Smith Date:August 6 1999 1:58am
Subject:Re: GET_LOCK .... more.... help!
View as plain text  
At 18:19, 19990805, Paul Wagorn wrote:
>you say:
>select get_lock($name,$timeout)
>where $name is an arbitrary name for the lock and
>$timeout is how long the lock lasts (if not released)

No.  $timeout is how long you want to wait for the lock before
giving up.  That is, if someone else holds the lock, you have
to wait until they release it before you can get it - that's
the point of locks.  But you don't want to wait forever, if
they're never going to release the lock.  Maybe you only want
to wait 5 seconds, and then ask your user if they'd like to
retry or not.  $timeout has nothing to do with how long you
hold the lock once you get it.  Some external event must take
place for the lock to be released - you release it explicitly
with RELEASE_LOCK($name), you perform another GET_LOCK() (using
the same name or a different one, it doesn't matter), or your
connection closes.

>how do I actually lock a certain record in a certain table??  ie:
>"Lock all the records that have "JIM" in the NAME column in the table
>EMPLOYEES, and call the lock NAMELOCK"

You have to translate, in your head, from the domain of record
locking into the domain of what GET_LOCK() offers you.  GET_LOCK()
offers you advisory locking based on a string of characters, and
that's it.  It doesn't lock any real thing, it's merely a convenience
function for your application to use.  Since it deals with strings
of characters, you have to find a way to map records in your database
to strings of characters.

Here's a possible example, but your setup might require something

    # wait up to 2 minutes to get a record lock on the Employee
    # named "Jim"
    SELECT GET_LOCK("Record Lock: EMPLOYEES: JIM", 120);

    if (result != 1)
        fail("Couldn't get lock for Employee Jim");

    # now you may safely update any records in EMPLOYEES where
    # the name is JIM


Why is it safe for you to update any employee named Jim, after
you have the lock?  Because you've programmed your application(s)
so that any one that is going to update (or read, perhaps, it
depends on your situation) the data in the EMPLOYEES table first
gets a lock based on the Name they're updating.  MySQL doesn't
enforce anything, it just provides a tool for your application to
enforce the locking.

>then, how do I do a select using the lock ie:
>"sleect records that are not locked by the lock called NAMELOCK"

This would be the same as saying, "Select * from EMPLOYEES where
Name <> 'JIM'".  So that's what you'd select.

GET_LOCK .... more.... help!Paul Wagorn6 Aug
  • Re: GET_LOCK .... more.... help!Thimble Smith6 Aug