"Fossies" - the Fresh Open Source Software Archive

Member "Apache-Session-1.93/lib/Apache/Session/Lock/MySQL.pm" (15 Sep 2009, 3860 Bytes) of package /linux/www/Apache-Session-1.93.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "MySQL.pm" see the Fossies "Dox" file reference documentation.

    1 ############################################################################
    2 #
    3 # Apache::Session::Lock::MySQL
    4 # MySQL locking for Apache::Session
    5 # Copyright(c) 2000 Jeffrey William Baker (jwbaker@acm.org)
    6 # Distribute under the Perl License
    7 #
    8 ############################################################################
    9 
   10 package Apache::Session::Lock::MySQL;
   11 
   12 use strict;
   13 
   14 use DBI;
   15 use vars qw($VERSION);
   16 
   17 $VERSION = '1.01';
   18 
   19 sub new {
   20     my $class = shift;
   21     
   22     return bless {lock => 0, lockid => undef, dbh => undef, mine => 0}, $class;
   23 }
   24 
   25 sub acquire_read_lock  {
   26     my $self    = shift;
   27     my $session = shift;
   28     
   29     return if $self->{lock};
   30 
   31     if (!defined $self->{dbh}) {
   32         if (defined $session->{args}->{LockHandle}) {
   33             $self->{dbh} = $session->{args}->{LockHandle};
   34         }
   35         else {
   36             $self->{dbh} = DBI->connect(
   37                 $session->{args}->{LockDataSource},
   38                 $session->{args}->{LockUserName},
   39                 $session->{args}->{LockPassword},
   40                 { RaiseError => 1, AutoCommit => 1 }
   41             );
   42             $self->{mine} = 1;
   43         }
   44     }
   45 
   46     local $self->{dbh}->{RaiseError} = 1;
   47 
   48     $self->{lockid} = "Apache-Session-$session->{data}->{_session_id}";
   49     
   50     #MySQL requires a timeout on the lock operation.  There is no option
   51     #to simply wait forever.  So we'll wait for a hour.
   52     
   53     my $sth = $self->{dbh}->prepare_cached(q{SELECT GET_LOCK(?, 3600)}, {}, 1);
   54     $sth->execute($self->{lockid});
   55     $sth->finish();
   56     
   57     $self->{lock} = 1;
   58 }
   59 
   60 sub acquire_write_lock {
   61     $_[0]->acquire_read_lock($_[1]);
   62 }
   63 
   64 sub release_read_lock {
   65     my $self = shift;
   66 
   67     if ($self->{lock}) {
   68         local $self->{dbh}->{RaiseError} = 1;
   69         
   70         my $sth = $self->{dbh}->prepare_cached(q{SELECT RELEASE_LOCK(?)}, {}, 1);
   71         $sth->execute($self->{lockid});
   72         $sth->finish();
   73         
   74         $self->{lock} = 0;
   75     } 
   76 }
   77 
   78 sub release_write_lock {
   79     $_[0]->release_read_lock;
   80 }
   81 
   82 sub release_all_locks  {
   83     $_[0]->release_read_lock;
   84 }
   85 
   86 sub DESTROY {
   87     my $self = shift;
   88     
   89     $self->release_all_locks;
   90     
   91     if ($self->{mine}) {
   92         $self->{dbh}->disconnect;
   93     }
   94 }
   95 
   96 1;
   97 
   98 =pod
   99 
  100 =head1 NAME
  101 
  102 Apache::Session::Lock::MySQL - Provides mutual exclusion using MySQL
  103 
  104 =head1 SYNOPSIS
  105 
  106  use Apache::Session::Lock::MySQL;
  107 
  108  my $locker = Apache::Session::Lock::MySQL->new();
  109 
  110  $locker->acquire_read_lock($ref);
  111  $locker->acquire_write_lock($ref);
  112  $locker->release_read_lock($ref);
  113  $locker->release_write_lock($ref);
  114  $locker->release_all_locks($ref);
  115 
  116 =head1 DESCRIPTION
  117 
  118 Apache::Session::Lock::MySQL fulfills the locking interface of 
  119 Apache::Session.  Mutual exclusion is achieved through the use of MySQL's
  120 GET_LOCK and RELEASE_LOCK functions.  MySQL does not support the notion
  121 of read and write locks, so this module only supports exclusive locks.  When
  122 you request a shared read lock, it is instead promoted to an exclusive
  123 write lock.
  124 
  125 =head1 CONFIGURATION
  126 
  127 The module must know how to connect to your MySQL database to acquire locks.
  128 You must provide a datasource name, a user name, and a password.  These options
  129 are passed in the usual Apache::Session style, and are very similar to the
  130 options for Apache::Session::Store::MySQL.  Example:
  131 
  132  tie %hash, 'Apache::Session::MySQL', $id, {
  133      LockDataSource => 'dbi:mysql:database',
  134      LockUserName   => 'database_user',
  135      LockPassword   => 'K00l'
  136  };
  137 
  138 Instead, you may pass in an already opened DBI handle to your database.
  139 
  140  tie %hash, 'Apache::Session::MySQL', $id, {
  141      LockHandle => $dbh
  142  };
  143 
  144 =head1 AUTHOR
  145 
  146 This module was written by Jeffrey William Baker <jwbaker@acm.org>.
  147 
  148 =head1 SEE ALSO
  149 
  150 L<Apache::Session>