in reply to Search engine code critique

The lack of $dbh->disconnect is a potential disaster waiting to happen. Don't know what the SQL server defaults are but by default MySQL will accept 100 connections that will persist for 8 hours. Although it surprised me undefing $dbh (ie when the script finishes) does not cause a DESTROY{ ..disconnect .. ) to be called allowing the connections to persist and accumulate.....until it falls over with a too many connections error. This particular error often goes unnoticed until you get real load on a widget.

One way to code it to make sure this does not happen is

my $sth; my $dbh->connect(...... END{ $sth->finish if $sth; $dbh->disconnect if $dbh } # now do whatever

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Search engine code critique
by bean (Monk) on Sep 19, 2003 at 03:26 UTC
    If ruhk is running mod_perl under Apache, this isn't a problem - the DBI connect method will default to Apache::DBI::connect which keeps persistent connections (in this case, disconnect is overloaded and will not disconnect). If this is a CGI, connect_cached should be used - a persistent connection would be appropriate for this application. However, disconnect-ing doesn't hurt and is good form...

      Actually if you use this syntax you will pile up persistent connections under mod_perl. Trust me. We have done it. In production. Disconnecting is much more than good form.....

      my $dbh = DBI->connect...

      The connection is as you say persistent. This is immaterial. This will pile connections up running under mod_perl/vanilla/or off the command line. The my means that the handle to the connection disappears every time you re-exec your script (effectively a sub). The connection however remains.....

      If you don't believe me put it in a loop and you will quickly bring your DB to its knees.

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        I can't argue with experience - your "does" beats my "should" (especially since I'm too lazy to check it myself). Sounds like a bug in Apache::DBI - it's supposed to keep track of instance/user/passwd (I think) combos and give you an existing connection if one matches/exists when you ask to connect. A copy of the connection handle is kept in Apache::DBI, so the "my" shouldn't make a difference - should it?
        Update
        Actually, it's more likely that something in your setup prevented Apache::DBI from being used by DBI - in which case the standard DBI::connect method would have been used, and without disconnects, connections would pile up.