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;

  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() 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.

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.