| 1 | # | ||
| 2 | # SessionLocker.pm - Manage session's lock for iNcom. | ||
| 3 | # | ||
| 4 | # This file is part of Apache::iNcom. | ||
| 5 | # | ||
| 6 | # Author: Francis J. Lacoste <francis.lacoste@iNsu.COM> | ||
| 7 | # | ||
| 8 | # Copyright (C) 2000 Francis J. Lacoste, iNsu Innovations | ||
| 9 | # | ||
| 10 | # This program is free software; you can redistribute it and/or modify | ||
| 11 | # it under the terms same terms as perl itself. | ||
| 12 | # | ||
| 13 | |||
| 14 | package Apache::iNcom::SessionLocker; | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | |||
| 18 | use DBI; | ||
| 19 | |||
| 20 | use vars qw($VERSION); | ||
| 21 | |||
| 22 | use constant MAX_TRIES => 5; | ||
| 23 | |||
| 24 | BEGIN { | ||
| 25 | ($VERSION) = '$Revision: 1.1 $' =~ /Revision: ([\d.]+)/ ; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub new { | ||
| 29 | my $class = shift; | ||
| 30 | |||
| 31 | my $x = 0; | ||
| 32 | |||
| 33 | return bless \$x, $class; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub acquire_read_lock { | ||
| 37 | acquire_lock( @_ ); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub acquire_write_lock { | ||
| 41 | acquire_lock( @_ ); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub release_read_lock { | ||
| 45 | my ( $self ) = @_; | ||
| 46 | |||
| 47 | return unless $$self; | ||
| 48 | return if --$$self; | ||
| 49 | |||
| 50 | release_lock( @_ ); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub release_write_lock { | ||
| 54 | my ( $self ) = @_; | ||
| 55 | |||
| 56 | return unless $$self; | ||
| 57 | return if --$$self; | ||
| 58 | |||
| 59 | release_lock( @_ ); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub release_all_locks { | ||
| 63 | my ( $self ) = @_; | ||
| 64 | |||
| 65 | return unless $$self; | ||
| 66 | |||
| 67 | release_lock( @_ ); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub release_lock { | ||
| 71 | my ( $self, $session ) = @_; | ||
| 72 | |||
| 73 | return unless $session->{args}{Serialize}; | ||
| 74 | |||
| 75 | my $dbh = $session->{args}{dbh} | ||
| 76 | or die "No opened database connection\n"; | ||
| 77 | |||
| 78 | my $sth =$dbh->prepare( qq{ UPDATE sessions SET locked_by = NULL | ||
| 79 | WHERE id = ? AND locked_by = ? } ); | ||
| 80 | |||
| 81 | $sth->execute( $session->{data}{_session_id}, $$ ); | ||
| 82 | $sth->finish; | ||
| 83 | $dbh->commit; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub acquire_lock { | ||
| 87 | my ( $self, $session ) = @_; | ||
| 88 | |||
| 89 | return unless $session->{args}{Serialize}; | ||
| 90 | |||
| 91 | # No need for lock in new session | ||
| 92 | return if $session->is_new; | ||
| 93 | |||
| 94 | # Only need one database lock | ||
| 95 | return if $$self++; | ||
| 96 | |||
| 97 | my $dbh = $session->{args}{dbh} | ||
| 98 | or die "No opened database connection\n"; | ||
| 99 | |||
| 100 | my $sth =$dbh->prepare( qq{ UPDATE sessions SET locked_by = ? | ||
| 101 | WHERE id = ? AND locked_by IS NULL } ); | ||
| 102 | |||
| 103 | my $sel_sth =$dbh->prepare( qq{ SELECT id FROM sessions | ||
| 104 | WHERE id = ? AND locked_by = ? } ); | ||
| 105 | |||
| 106 | my $try = 0; | ||
| 107 | my $success = 0; | ||
| 108 | while ( ! $success ) { | ||
| 109 | if ( $sth->execute( $$, $session->{data}{_session_id} ) ) { | ||
| 110 | $dbh->commit; | ||
| 111 | # Make sure that we have the lock | ||
| 112 | $sel_sth->execute( $session->{data}{_session_id}, $$ ); | ||
| 113 | |||
| 114 | my $results = $sel_sth->fetchrow_arrayref; | ||
| 115 | $success = defined $results; | ||
| 116 | } | ||
| 117 | last if $success; | ||
| 118 | |||
| 119 | die "Time out while waiting for session lock\n" | ||
| 120 | if ( $try++ > MAX_TRIES ); | ||
| 121 | sleep 1; | ||
| 122 | } | ||
| 123 | $sth->finish; | ||
| 124 | $sel_sth->finish; | ||
| 125 | } | ||
| 126 | |||
| 127 | 1; | ||
| 128 | __END__ |