| 1 | ############################################################################ | ||
| 2 | # | ||
| 3 | # Apache::Session::Lock::Sybase | ||
| 4 | # Sybase locking for Apache::Session | ||
| 5 | # Copyright(c) 2003 Oliver Maul (oli@42.nu) | ||
| 6 | # Distribute under the Perl License | ||
| 7 | # | ||
| 8 | ############################################################################ | ||
| 9 | |||
| 10 | package Apache::Session::Lock::Sybase; | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | |||
| 14 | use DBI; | ||
| 15 | use vars qw($VERSION); | ||
| 16 | |||
| 17 | $VERSION = '1.00'; | ||
| 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 | my $sth = $self->{dbh}->prepare(q{sp_getapplock @Resource = '}.$self->{lockid}.q{',@LockMode='Exclusive',@LockOwner='Session',@LockTimeout = '3600000'}); | ||
| 51 | $sth->execute() || die $DBI::errstr; | ||
| 52 | |||
| 53 | $self->{lock} = 1; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub acquire_write_lock { | ||
| 57 | $_[0]->acquire_read_lock($_[1]); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub release_read_lock { | ||
| 61 | my $self = shift; | ||
| 62 | |||
| 63 | if ($self->{lock}) { | ||
| 64 | local $self->{dbh}->{RaiseError} = 1; | ||
| 65 | |||
| 66 | my $sth = $self->{dbh}->prepare(q{sp_releaseapplock @Resource='}.$self->{lockid}.q{',@Lookowner='Session'}); | ||
| 67 | $sth->execute() || die $DBI::errstr; | ||
| 68 | |||
| 69 | $self->{lock} = 0; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | sub release_write_lock { | ||
| 74 | $_[0]->release_read_lock; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub release_all_locks { | ||
| 78 | $_[0]->release_read_lock; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub DESTROY { | ||
| 82 | my $self = shift; | ||
| 83 | |||
| 84 | $self->release_all_locks; | ||
| 85 | |||
| 86 | if ($self->{mine}) { | ||
| 87 | $self->{dbh}->disconnect; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | 1; | ||
| 92 | |||
| 93 | =pod | ||
| 94 | |||
| 95 | =head1 NAME | ||
| 96 | |||
| 97 | Apache::Session::Lock::Sybase - Provides mutual exclusion using Sybase | ||
| 98 | |||
| 99 | =head1 SYNOPSIS | ||
| 100 | |||
| 101 | use Apache::Session::Lock::Sybase; | ||
| 102 | |||
| 103 | my $locker = Apache::Session::Lock::Sybase->new; | ||
| 104 | |||
| 105 | $locker->acquire_read_lock($ref); | ||
| 106 | $locker->acquire_write_lock($ref); | ||
| 107 | $locker->release_read_lock($ref); | ||
| 108 | $locker->release_write_lock($ref); | ||
| 109 | $locker->release_all_locks($ref); | ||
| 110 | |||
| 111 | =head1 DESCRIPTION | ||
| 112 | |||
| 113 | Apache::Session::Lock::Sybase fulfills the locking interface of | ||
| 114 | Apache::Session. Mutual exclusion is achieved through the use of Sybase's | ||
| 115 | sp_getapplock and sp_releaseapplock functions. Sybase does not support the | ||
| 116 | notion | ||
| 117 | of read and write locks, so this module only supports exclusive locks. When | ||
| 118 | you request a shared read lock, it is instead promoted to an exclusive | ||
| 119 | write lock. | ||
| 120 | |||
| 121 | =head1 CONFIGURATION | ||
| 122 | |||
| 123 | The module must know how to connect to your MySQL database to acquire locks. | ||
| 124 | You must provide a datasource name, a user name, and a password. These options | ||
| 125 | are passed in the usual Apache::Session style, and are very similar to the | ||
| 126 | options for Apache::Session::Store::Sybase. Example: | ||
| 127 | |||
| 128 | tie %hash, 'Apache::Session::Sybase', $id, { | ||
| 129 | LockDataSource => 'dbi:sybase:database', | ||
| 130 | LockUserName => 'database_user', | ||
| 131 | LockPassword => 'K00l' | ||
| 132 | }; | ||
| 133 | |||
| 134 | Instead, you may pass in an already opened DBI handle to your database. | ||
| 135 | |||
| 136 | tie %hash, 'Apache::Session::Sybase', $id, { | ||
| 137 | LockHandle => $dbh | ||
| 138 | }; | ||
| 139 | |||
| 140 | =head1 AUTHOR | ||
| 141 | |||
| 142 | This module was written by Oliver Maul <oli@42.nu>. | ||
| 143 | |||
| 144 | =head1 SEE ALSO | ||
| 145 | |||
| 146 | L<Apache::Session> |