filmo has asked for the wisdom of the Perl Monks concerning the following question: (database programming)

How do I lock tables using DBI and mySQL?

Originally posted as a Categorized Question.

  • Comment on How do I lock tables using DBI and mySQL?

Replies are listed 'Best First'.
Re: How do I lock tables using DBI and mySQL?
by blakem (Monsignor) on Sep 11, 2001 at 08:51 UTC
    See the Lock Tables syntax in the MySQL documentation. Then wrap each sql query inside of something like:
    $dbh->do(" ... ");
Re: How do I lock tables using DBI and mySQL?
by filmo (Scribe) on Sep 11, 2001 at 09:01 UTC
    Follow up question: Does the lock cover multiple statement handles? i.e.
    $dbh->do("LOCK TABLES foo WRITE, bar READ"); $sth->prepare("SELECT x,y,z FROM bar"); $sth2->prepare("INSERT INTO foo SET a = ?"); while (@ary = $sth->fetchrow_array()) { $sth2->$execute($ary[0]); } $sth2->finish(); $sth->finish(); $dbh->do("UNLOCK TABLES");
    Is this correct or will $sth2 not be able to access the tables because of the lock?
Re: How do I lock tables using DBI and mySQL?
by blakem (Monsignor) on Sep 11, 2001 at 10:07 UTC
    Yes, I that should work... Table locking is done at the per-thread level, and I think both statement handles above exist within the same thread.