XML::Comma::Pkg::MySQL_Simple_Lock - A simple one-time-use lock coordinated by db
use XML::Comma::Pkg::MySQL_Simple_Lock; my $lock = XML::Comma::Pkg::MySQL_Simple_Lock->new ( "foo", 10 ); die "couldn't get lock\n" unless $lock; undef $lock
Comma applications can be spread across multiple machines in a cluster, as long as core database operations are all pointed at a centralized server. This module provides advisory locking capabilities to Comma programmers working in such an environment.
MySQL exposes a very simple advisory locking API, the functions GET_LOCK() and RELEASE_LOCK(). This module provides a very simple, object-oriented abstraction on top of these functions. Here is a simple test script:
#!/usr/bin/perl -w use strict; $|++; use XML::Comma::Pkg::MySQL_Simple_Lock; &do_lock; print "waiting outside scope... "; my $junk = <>; sub do_lock { my $lock = XML::Comma::Pkg::MySQL_Simple_Lock->new ( "foo", 1 ); die "couldn't get lock\n" unless $lock; print "$lock\n"; print "waiting inside scope... "; my $junk = <>; }
The new() method expects a key_string argument, which is used to "name" the lock, and an optional timeout argument. timeout should be given in seconds, and defaults to 86400, or one day.
new()
key_string
timeout
new() returns a lock object on success, or undef on failure.
The lock is held as long as a reference to the MySQL_Simple_Lock object is being held. When all references to the object go out of scope, the object is destroyed and the lock is automatically released. It is a good idea to explicitly undef any lock objects, to make life easier for maintenance programmers.
undef
Each MySQL_Simple_Lock object is a one-time-use construct. To request a new lock, make a new object.
Each MySQL_Simple_Lock object occupies a database connection as long as it is held.
comma@xml-comma.org
http://xml-comma.org
To install XML::Comma, copy and paste the appropriate command in to your terminal.
cpanm
cpanm XML::Comma
CPAN shell
perl -MCPAN -e shell install XML::Comma
For more information on module installation, please visit the detailed CPAN module installation guide.