You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by David Hofmann <mo...@hotmail.com> on 2004/08/11 16:01:36 UTC

Apache::DBI Connection Lost

For some reason whenever I restart my Database process the web server loses 
it's connection and doesn't reconnect after the database comes back up.

Bellow is the Apache Startup script, The connect module I use, and the 
connection string I use in my perl scripts.

Any suggestion on why Apache::DBI won't reconnect ?

David Hofmann

--- My Apache Startup Script ---

# make sure we are in a sane environment.
$ENV{MOD_PERL} or die "not running under mod_perl!";

use strict;
use Carp ();
use Apache::DBI ();
DBI->install_driver("Pg");
# $Apache::DBI::DEBUG = 0;
Apache::DBI->connect_on_init("DBI:Pg:dbname=SERVERNAME;host=HOSTNAME;port=PORT;","USERNAME", 
"PASSWORD")
	or die "Cannot connect to database: $DBI::errstr";
1; #return true value

-- End Starup --

-- My Connect Module --

package MY::DBConnect;

use strict;
use Carp;
if ($ENV{MOD_PERL}) {
	use Apache::DBI;
}
use DBI ();
use MY::Error;

sub connect {
	if (defined $MY::DBConnect::conn) {
		eval {
			$MY::DBConnect::conn->ping;
		};
		if (!$@) {
			return $MY::DBConnect::conn;
		}
	}

	### Perform the connection using the driver
	$MY::DBConnect::conn = DBI->connect( 
"dbi:Pg:dbname=SEVERNAME;host=HOSTNAME-;port=PORT","USERNAME", "PASSWORD")
		or &error('0','EN','CONNECT_SQL','CONNECT_SQL','The Support site is 
currently experiencing technical difficulties. Please try again later.  We 
apologize for any inconvenience.',1 );
	return $MY::DBConnect::conn;
}

1;

-- End Connect Module --

-- Connection sting in scripts --

use DBI ();
use MY::DBConnect;

### Perform the connection using the driver
my $dbh ||= &MY::DBConnect::connect;

-- End Scripts stuff --

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Apache::DBI Connection Lost

Posted by Perrin Harkins <pe...@elem.com>.
On Wed, 2004-08-11 at 10:01, David Hofmann wrote:
> Any suggestion on why Apache::DBI won't reconnect ?
> 
> David Hofmann
> 
> --- My Apache Startup Script ---
> 
> # make sure we are in a sane environment.
> $ENV{MOD_PERL} or die "not running under mod_perl!";
> 
> use strict;
> use Carp ();
> use Apache::DBI ();
> DBI->install_driver("Pg");

Try taking that install_driver call out.  That caused problems for me
with DBD::Oracle.

Also, make sure you are not doing any connections during startup.  I
remember DBD::Oracle had trouble reconnecting if I connected before
forking, even though the connections during startup are not persistent.

- Perrin


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html