mp3car-2001 has asked for the wisdom of the Perl Monks concerning the following question:

The DBI DBD for MySQL can do table level locking. If I lock a table, and another program tries to access it, what happens? I know I should just try it and see, but what I'm looking for is what is supposed to happen. Does it just block until it becomes unlocked, or is it supposed to return a failure?

Thanks,
Joe

Replies are listed 'Best First'.
Re: DBI and table locking
by salvadors (Pilgrim) on Jan 07, 2001 at 18:30 UTC

    If I lock a table, and another program tries to access it, what happens? ... Does it just block until it becomes unlocked, or is it supposed to return a failure?

    This very much depends on what sort of lock you take on a table. If you take a READ lock, then everything else will still be able to read the table also, but any updates to it will be stalled until you release your lock.

    If you take a WRITE lock, however, then nothing can read or write the table(s) concerned until you release your lock.

    All locked queries will just stall, waiting for the lock to release, rather than failing.

    Tony

      Thanks a lot. This answers my question and I know that I can safely WRITE lock my table for a quick operation and I don't need to provide error handling for other programs that access this table. Many thanks.
        But if you are grabbing several locks be careful about what order you do it in. If programs X and Y grab locks on tables A and B respectively, and then try to lock B and A, they get into this pathetic staring contest called a deadlock. This is generally a Bad Thing...
Re: DBI and table locking
by dealgan (Novice) on Jan 08, 2001 at 18:03 UTC
    One thing to bear in mind is that a good table design will often save you having to do any table locking. I have had some horrible experiences with having to lock tables which would have been avoided had the tables been well designed.

    At this point I have to admit that most of the tables were designed by me, back when I knew less.

    A decent primary key on a table can save a whole load of heartache later I can promise you :)