DBI funkiness

gav on 2003-12-12T18:55:24

I was having problems with an application that created loads of DBI handles which just sat around sleeping. Enabling MySQL's query log let me trace through what each connection was doing and I tracked it down to a sub that was like this:

sub do_something {
    my $dbh = init_dbh();
    # do something with dbh
    $dbh->disconnect;
    if ($something) {
        # do something else with dbh
    }
}

The issue was that instead of causing an error, DBI was magically connecting back to the database that we disconnected from when we executed the next query. This handle was then never being disconnected. Is this a bug? The DBI documentation suggests that "The handle is of little use after disconnecting", and that the handle will be disconnected when the handle goes out of scope.


auto_reconnect

vsergu on 2003-12-15T20:43:32

I think your problem is related to the auto_reconnect setting in DBD::mysql, which was changed recently so that it doesn't happen by default. Are you using a version of DBD::mysql older than 2.9002?

Some of my applications were depending on the reconnection, so they started breaking when I upgraded. I'm still trying to figure out how to get the old behavior, since setting auto_reconnect doesn't seem to solve the problem completely.

Re:auto_reconnect

gav on 2003-12-15T23:17:12

I'm using 2.9002 which according to the ChangeLog turns on auto_reconnect if $ENV{MOD_PERL} is set. Now I know what's up I'll make sure it's turned off as that seems to make more sense.