LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (http://www.linuxquestions.org/questions/linux-server-73/)
-   -   Perl Apache Session MySQL inconsistency on Debian Lenny (http://www.linuxquestions.org/questions/linux-server-73/perl-apache-session-mysql-inconsistency-on-debian-lenny-874314/)

merlininthewood 04-11-2011 09:49 AM

Perl Apache Session MySQL inconsistency on Debian Lenny
 
Hi

I have been tearing my hair out with this. As part of a backend perl script i am holding the Epoch time as session data using apache session and mysql so i can delete sessions after an expiry period. When I retrieve the time it is about three months old even when i only just set it. The code example below best describes the problem:

I have got the table set up that Apache session requires.
Code:

#!/usr/bin/perl

use strict;
use DBI;
use Apache::Session::MySQL;

my %config;

  $config{'dbname'} = "somedatabase";                                # Database name
  $config{'dbuser'} = "root";                        # Database user
  $config{'dbpass'} = "secret";                                # Database password
 
my $dbhg = DBI->connect("dbi:mysql:$config{'dbname'}",$config{"dbuser"},$config{"dbpass"})
        or die "Could not open MySQL DataBase - SQL Error: $DBI::errstr\n";
       
my %session;

tie %session, 'Apache::Session::MySQL', undef, {Handle=>$dbhg, LockHandle=>$dbhg};

my $sessid = $session{_session_id};

print "New session:$sessid\n";
       
$session{'time'} = time;

print "Time set: $session{'time'}\n";

untie %session;

#### Now to retie the session and retrieve the time

tie %session, 'Apache::Session::MySQL', $sessid, {Handle=>$dbhg, LockHandle=>$dbhg};

print "Connected session:$session{_session_id}\n";

my $retrievedTime = $session{'time'};
       
print "Retrieved time: $retrievedTime\n";

untie %session;

This gives the output:

Code:

New session:14199f84b1e646b528d63b3be180a9b1
Time set: 1302529444
Connected session:14199f84b1e646b528d63b3be180a9b1
Retrieved time: 1295975743

This is happening on 3 out of four machines all running Debian. It works fine on the one!?!?!

Anyone got any ideas?

mgichoga 04-12-2011 12:35 AM

Ran the same code and works for me. I'm running ubuntu 10.10 You could try add a 'undef %session' right after the first 'untie %session' so that it looks like this:

Quote:

untie %session
undef %session
I don't know if this will lead to other problems with the key=>values pairings but might be a troubleshooting tool.

Also are you running the MySQL database and the perl script on the same machine? Have you checked time on local machine...probably so.

Michael

merlininthewood 04-12-2011 05:44 AM

Thanks for your reply. I tried using undef %session but with the same result. The actual script i am using this in is a mod perl script that is called on separate occasions between setting and retrieving the session data (hence the need to use apache session!!). I wrote the script above to see if it was a problem with my main script or mod perl but the phenomenon occurs on this simple perl script also. It always returns a time some time in the afternoon (but different times) on Tuesday the 25th January 2011. How weird is that?

Two of the machines it occurs on (and the one where it works OK) are running Apache::Session 1.86 and the other 1.87 (upgraded to Squeeze). The database is run on the same machine as the script in all cases.

I have googled but found no-one who has come across a similar problem.

merlininthewood 04-13-2011 03:52 PM

It gets even more weird. I started poking around inside Session.pm to see where the inconsistency starts. I added some print commands to give me an idea what the values are as they are processed by apache session and it started working as it should. I narrowed it down to adding a print command in the STORE routine:
Code:

sub STORE {
    my $self  = shift;
    my $key  = shift;
    my $value = shift;
    print "VALUE=$value\n"; #####################
    $self->{data}->{$key} = $value;
   
    $self->{status} |= MODIFIED;
   
    return $self->{data}->{$key};
}

Now my script returns:
Code:

New session:fa8aa2bc8531313007e0463920fbd25d
VALUE=1302723468
Time set: 1302723468
Connected session:fa8aa2bc8531313007e0463920fbd25d
Retreived time: 1302723468

As it should. And when i remove the print statement again:
Code:

New session:92344b19524a01f524bd6afe56bbdec0
Time set: 1302723485
Connected session:92344b19524a01f524bd6afe56bbdec0
Retreived time: 1295990591

Can anyone think why printing the $value string would make it behave differently? I tried setting 'my $tmp=$value;' as an ugly fix but that didn't work.

chrism01 04-14-2011 12:45 AM

If you're really sure you've found a bug in Session.pm, you can notify the maintainer; look up the module at search.cpan.org http://search.cpan.org/~rsoliv/Session-0.01/Session.pm.
Also, you may want to raise this at perlmonks.org; where all the Perl gurus hang out.

merlininthewood 04-14-2011 08:47 AM

I have raised this on perlmonks.org if anyone wants to follow it there.


All times are GMT -5. The time now is 09:08 PM.