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

hello wise men !

hope you can answer my question...

I've a perl web-application running on mod_perl2.0.4 and Apache 2.2.10.
In the http.conf this entry is - for sure - commented out:
#PerlModule Apache::DBI

If i use the /server-info page from apache it's showing me the loaded modules:

mod_perl.c, mod_setenvif.c, mod_rewrite.c, mod_negotiation.c, mod_mime.c, mod_log_config.c, mod_isapi.c, mod_info.c, mod_include.c, mod_expires.c, mod_env.c, mod_dir.c, mod_cgi.c, mod_autoindex.c, mod_authz_user.c, mod_authz_host.c, mod_authz_groupfile.c, mod_authz_default.c, mod_authn_file.c, mod_authn_default.c, mod_auth_basic.c, mod_asis.c, mod_alias.c, mod_actions.c, mod_so.c, http_core.c, mpm_winnt.c, mod_win32.c, core.c

so, again no Apache::DBI.

Now the interesting part:

I open a database connection in my appliaction like this: $dbh = DBI->connect_cached($dsn, $db_user_name, $db_password, {AutoCommit => 0, PrintError => 0, PrintWarn => 0}) or die $DBI::errstr;

$dbh is a global variable which is used in other modules.


I am not issuing a $dbh-> disconnect.
I don't know why i should do that

So i would guess now, that the database-connection is closed after every request - but it's not, which a can tell from the mysql logs.


So here is the code:


# global database-handlers
our $dbh;
our $dbh_sessions;

sub checkDbh {
if (defined($dbh)) {
if ($dbh->ping == 1) {
# ok
}
else {
initDbh();
}
}
else {
initDbh();
}
}




sub initDbh {
undef($dbh);
my $dsn="DBI:mysql:travel:localhost";
my $db_user_name = $travelproject::props->getProperty('db_user');;
my $db_password = $travelproject::props->getProperty('db_password');;
eval { $travelproject::log->info("establishing db-connection(application)...");
$travelproject::log->info("dsn: $dsn");
$travelproject::log->info("db_user: $db_user_name");
$dbh = DBI->connect($dsn, $db_user_name, $db_password, {AutoCommit => 0, PrintError => 0, PrintWarn => 0}) or die $DBI::errstr;
};
if ($@) {
my $error = $@;
$travelproject::log->error($error);
$travelproject::log->error("Database connection not made: $DBI::errstr");
undef($dbh);
return;
}
else {
$travelproject::log->info("successfully connected to database");
}
# check if autocommit is really disabled
if ($dbh->{'AutoCommit'}) {
my $error = "unable to turn of autocommit";
$travelproject::log->error($error);
undef($dbh);
return;
}
}


# for every new request coming in i call:
checkDbhs();

-------------------------------------------------
This database-handle seems to stay there forever,
when i don't call ->disconnect on it.
So any clue why i have a persistant connection even though i am not using Apache::DBI ?

I am confused now...
Can you help me ?
  • Comment on persistent database connection without Apache::DBI

Replies are listed 'Best First'.
Re: persistent database connection without Apache::DBI
by roboticus (Chancellor) on Aug 15, 2009 at 13:12 UTC
    hoggle:

    I'm not a web programmer, nor do I play one on T.V. But I'd guess that since Apache handles multiple requests at the same time, that your successive web requests are handled by different processes, so while the connection is still established, it's not by the same process. I suspect that a Super Search might turn up some similar questions, complete with answers or pointers to solutions.

    ...roboticus
Re: persistent database connection without Apache::DBI
by Anonymous Monk on Aug 15, 2009 at 03:52 UTC
    ... /server-info page .... so, again no Apache::DBI.

    mod_info doesn't provide that information. You want to check /perl-status

Re: persistent database connection without Apache::DBI
by codonnell (Initiate) on Sep 14, 2009 at 18:56 UTC
    hi.

    i'm sure if you already figured this out, but your connection is persistent because you are using DBI->connect_cached() rather than DBI->connect().

    $dbh = DBI->connect_cached(...) # persistent $dbh = DBI->connect(...) # not persistent

    best,

    chuck