NAME

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

SYNOPSIS

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.
undef($locker);

DESCRIPTION

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.

CLASS METHODS

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.

Parameters:

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

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

DESTROY

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

HISTORY

Version 1.00 2002-03-26

Initial version

AUTHOR

Craig Manley <cmanley at cpan dot org>

COPYRIGHT

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.

SEE ALSO

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