Mysql::NameLocker - Safe way of locking and unlocking MySQL tables using named locks.


 use Mysql::NameLocker;

 # Simulate a record lock
 my $tablename = 'category'
 my $id = 123;
 my $lockname = "$tablename_$id";
 my $timeout = 10;
 my $locker = new Mysql::NameLocker($dbh,$lockname,$timeout);

 # Execute some tricky statements here...

 # Locks are automically released when $locker goes out of scope.


Mysql::NameLocker is a simple class for safely using MySQL named locks. A locks is created when you instantiate the class and is automatically released when the object goes out of scope (or when you call undef on the object). One situation where this class is useful is when you have persistent database connections such as in some mod_perl scripts and you want to be sure that locks are always released even when a script dies somewhere unexpectedly.


new ($dbh,$lockname,$timeout)

Attempts to acquire a named lock and returns a Mysql::NameLocker object that encapsulates this lock. If a timeout occurs, then undef is returned. If an error occurs (The MySQL statement GET_LOCK() returns NULL) then this constructor croaks.


1. DBI database handle object.
2. Lock name.
3. Timeout in seconds.

Returns: Mysql::NameLocker object or undef if failed to acquire lock.


Destructor called implicitly by perl when object is destroyed. The acquired lock is released here if the DBI database handle is still connected.


Version 1.00 2002-03-26

Initial version


Craig Manley <cmanley at cpan dot org>


Copyright (C) 2001 Craig Manley. All rights reserved. This program is free software; you can redistribute it and/or modify it under under the same terms as Perl itself. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


The MySQL documentation about GET_LOCK() and RELEASE_LOCK().