PB has asked for the wisdom of the Perl Monks concerning the following question:

Hi All

I have the following setup
Apache 1.3.23
Mod_perl 1.26
Apache::DBI 0.88
AuthMySQL 2.20
Perl 5.6.0
MySQL 3.23.49
This all seems to work fine, except for one problem, which is the following:

I have and html page with a form which passes all its fields to a Perl CGI script . The perl script then does a connect to the MySQL database and adds all the fields. This works fine for the 1st 7 times, then on the 8th time the same values that were passed for the first connecion are passed again to the database.

What I have noticed in the logs is that the 1st seven invocations of the cgi script create a new PERSISTENT database connection thereafter they are reused maintaining all the previous variables that form passed to the perl script on first use.

All my variables in are local (declared with my)

Please help
Thanks
Paul
  • Comment on Persistent database connections show incorrect results

Replies are listed 'Best First'.
Re: Persistent database connections show incorrect results
by Fletch (Bishop) on Apr 17, 2002 at 18:53 UTC

    ITYM `lexical', not local.

    At any rate, presuming you're running a CGI under Apache::Registry (since you provide no code . . .) any file scope my varibles will only be initialized once when it's initially read in. See Global Variables Persistence in the mod_perl guide.

Re: Persistent database connections show incorrect results
by perrin (Chancellor) on Apr 17, 2002 at 19:17 UTC
    It's hard to tell what the problem is without seeing any code, but I'm guessing it happens as soon as you hit any child process the second time. It probably has to do with something being persistent that you didn't expect. Check it by running in single-process mode: httpd -X. It's probably not related to Apache::DBI or persistent database connections.
      Thanks for the respones.
      Yes I mean lexical .
      I tried running httpd -X, and then it happens immediately ie: the second time I submit the form it has the values from the first time.

      Here is the code.

      use strict vars; use CGI qw(param); use diagnostics ; # Constants my $db = "ecreditdb"; my $server = "localhost"; my $user = "updater"; my $password = "1q2w3e4r"; # CGI Parameters my $Id = param("ID"); my $Fname = param("Fname"); sub init { # Connect to database my $dbh = ''; $dbh = DBI->connect("DBI:mysql:$db:$server", $user,$password); if (! $dbh) { print "Could not connect to $db on $server\n"; } $dbh->{RaiseError} = 1; # Prepare and run the query my $query="INSERT INTO clients values( \"$Fname\",\"$Id\") ;"; my $sth = $dbh->prepare_cached("$query") or die "Can't prepare stateme +nt: $dbh->errstr \n"; $sth->execute or die "Can't execute statement", $dbh->errstr ; $sth->finish; $dbh->disconnect(); disp(); } # End sub disp { print " <html> <head> <title>Update completed successfully</title> </head> <body bgcolor=\"#CCCCCC\"> <p align=\"center\"><font face=\"Courier New\" size=\"4\"><b><br> <font color=\"#000000\">Update completed successfully</font></b></font +></p> <p align=\"center\">&nbsp;</p> <p align=\"center\"> <b><font color=\"#8000000\">Please logon again !! </font></b></p> <p align=\"center\">&nbsp;</p> <p align=\"center\"> <a href=\"/private/login.htm\">Press here to logon</a></p> </body> </html> "; } init();


      Thanks
        You have am unintentional closure, created by using subroutines that refer to lexicals declared outside their scope. This is a known problem with Apache::Registry (but not with standard mod_perl handlers written to the mod_perl API). You can fix it by moving the CGI parameter code inside the init sub.

        For more on this topic, see the guide. Also, turn on warnings use full strict (not just vars) to catch this sort of problem.