You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by "DJ (David J Radunz)" <dj...@webmastery.com.au> on 2001/09/13 07:16:44 UTC

DBI connections build up..

Greetings,

    I am having a problem with a module im writing connecting to the database everytime its run, and not cleaning up the database connection when its finished. I have tried the 2 scenarios below and as yet i cannot work out how to stop the connections building up and eventually causing mysqld to crash.

Scenario 1:

in the script:  (NB: this is not the entire script.. just snippets to show what im trying to achive).

use strict;
use vars ($dbh);

use mod_perl;                                                     
use DBI;

sub config {
.............

    $dbh ||= &dbi_connect;
}

sub dbi_connect { 
        my $an = $C{'auth_name'};
        my $dbh = DBI->connect("$C{$an}{'TicketDB'}", "$C{$an}{'TicketDBUser'}", "$C{$an}{'TicketDBPassword'}")
                or die "Couldn't connect to database: $!";
        return $dbh;
}

sub login_handler {
    my ($self, $r) = @_;
    &config($r);
    ........
}

1;

END {
    $dbh->disconnect;
}



Scenario 2:

same script as above, except Apache::DBI was used... the result is the same.. the database connections just keep building up. 


Please help, i dont know if i have been descriptive enough about the problem. But I need to resolve this, as when the site gets a few visits.. mysql and apache crash from having too many connections to the database.

DJ
Webmastery.

Re: DBI connections build up..

Posted by Perrin Harkins <pe...@elem.com>.
> "DJ (David J Radunz)" wrote:
>     I am having a problem with a module im writing connecting to the
> database everytime its run, and not cleaning up the database
> connection when its finished.

Your script uses an END block to disconnect.  If you use
Apache::Registry, that will not be run until the server is shut down. 
With Apache::DBI, you should get one connection per Apache process, and
they'll stay open.  If you are changing the login parameters (i.e.
different user each time), you can't use Apache::DBI because you'll get
a huge build-up of connections.

- Perrin

Re: DBI connections build up..

Posted by Jeff Beard <je...@cyberxape.com>.

On Thu, 13 Sep 2001, DJ (David J Radunz) wrote:

> use strict;
> use vars ($dbh);

You don't need this with Apache::DBI. Globals in general should be
avoided/used with extreme caution.

> use mod_perl;

Don't need this either.

> 1;

> END {
>     $dbh->disconnect;
> }

Put this before the '1;' or just don't use it.

Read the guide front to back: http://perl.apache.org/guide/

HTH.

Jeff


--
Jeff Beard
_______________________________________
Web:		www.cyberxape.com
Email:		jeff at cyberxape dot com
Earth:		Boulder, CO, USA


Re: DBI connections build up..

Posted by Ged Haywood <ge...@www2.jubileegroup.co.uk>.
Hi there,

On Thu, 13 Sep 2001, DJ (David J Radunz) wrote:

> the database connections just keep building up. 

Read the database section of the Guide:

http://perl.apache.org/guide

73,
Ged.