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

I have a weird prblem: i have a module that makes a connection to a database and process the queries. in the command line everything woks ok but when i run use the module on a mod_perl script it can't connect. The message i get is this: connect: Network is unreachable the database server is rechable because with the command line test programs i can make the connection.

i tried connecting using directly the DBI methods in muy mod_perl handler but it still does not work, it gives me the same error.

I don't really know what to do in here i have updated the DBI module but it still not working.


Thanks for your help.


ignorance, the plague is everywhere
--guttermouth

Replies are listed 'Best First'.
Re: mod_perl 2 DBI problem
by dynamo (Chaplain) on May 04, 2005 at 22:54 UTC
    Most of the mod_perl specific DBI issues I've heard about have to do with things being set up more than once, since they are not automatically re-initialized each time through. It seems like an odd problem to be having that you can't connect at all when running under mod_perl. My first guess is that some other part of the script is having trouble because it's variables are not properly initialized.. It's hard to tell though without seeing the relevant parts of your module.

    Look at the input to the DBI functions, see if there are variables there that could be causing the problem, and do sanity checks by logging or debugging to screen to make sure the parameters are what you expect them to be.

    Also, you say your script works from the command line - is it exactly the same script you are running from mod_perl? Usually you'd at least have to be doing a '-e handler' from the command line to make that work, and simulate the apache request obj and the rest of the mod_perl interface. Perhaps your module isn't fully adapted to work in mod_perl? What command line do you use?

    Another thought. If you know it works through the command line, as a half-way measure, try just calling the script through the shell with backticks or system(). At least you'll have more context on when your database connection works and when it doesn't.

    It's hard to give a better response without some code to look at.

      The code is kind of large to paste it all so i'll paste the relevant to the connection:


      this module is loaded from a module that is loaded on a mod_perl handler

      package Suae; # bunch of functions..... #Function that establishes the connection #my $CONEXION = "dbi:Sybase:server=$host"; my $CONEXION = "dbi:Pg:dbname=mydb;host=myhost.com"; # print "<br><b>Suae::DEBUGE conectando: dbi:Pg:dbname=avc;host= +myhost.com</b><br>"; # print STDERR "Suae::DEBUGE conectando: dbi:Pg:dbname=avc;host= +myhost.com\n"; my $suaedbh = DBI->connect($CONEXION,"user","pass"); if(!defined($suaedbh)){ $r->log_error("Error connecting to the database ($DBI::errstr)\ +n"); return(undef); }


      This works when i call the module from within a test program, but it does not work when the module is loaded from a mod_perl handler


      ignorance, the plague is everywhere
      --guttermouth