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

Hello all.

This seems like it should be a simple problem, and I get the feeling I'm missing something obvious.

Preamble: I'm running ActiveState's Perl v5.8.7 on Windows XP. I have DBD::Mysql v3.0002 and DBI v1.48, installed via PPM. My MySql server is v3.23.54, running on Redhat Linux.

My understanding of mysql_auto_reconnect from the DBD::Mysql docs is that it should automatically reconnect if the server has timed out a connection.

However, in this simple test case:

use strict; use DBI; my $mysql = DBI->connect('DBI:mysql:database=mydb;host=myhost;port=330 +6', 'user', 'password', {RaiseError => 1}); $mysql->{mysql_auto_reconnect} = 1; while (1) { my $query = $mysql->prepare("select count(*) from sometable"); $query->execute(); my ($count) = $query->fetchrow_array; printf "Found %d records.\n", $count; sleep 5*60; # In reality, my program does lots of other things her +e that may take a while. }

It works correctly the first time, but the second time through the loop, I get:

DBD::mysql::st execute failed: Lost connection to MySQL server during +query at foo.pl line 11. DBD::mysql::st execute failed: Lost connection to MySQL server during +query at foo.pl line 11.

(Yes, I did say that twice. (Echo. Echo. :-)))

The server is set to time out after 4 minutes, but why isn't the client auto-reconnecting as necessary?

Am I calling mysql_auto_reconnect in the wrong way? Am I misunderstanding what it's meant to do? Any thoughts will be greatly appreciated.

Many thanks,
Neil.

Edit: g0n - replaced pre tags with code tags

Replies are listed 'Best First'.
Re: DBD::MySql - Problem with mysql_auto_reconnect not auto reconnecting.
by wazzuteke (Hermit) on Nov 25, 2005 at 17:29 UTC
    I might have a couple of ideas that will hopefully help.

    1. From reading the POD for DBD::mysql, it notes the following:

    This attribute is ignored when AutoCommit is turned off, and when AutoCommit is turned off, DBD::mysql will not automatically reconnect to the server.

    Although, after DBD::mysql version 2.0416, this should default to 'on', you might want to try $handle->{'AutoCommit'} = 1; just in case.

    2. You might want to verify that the version of DBD::mysql is recent. I know more and more functionality has been built into this module with later releases of MySQL

    3. I haven't found this to be true for this specific attribute, however you might want to look into verifying that auto_reconnect is supported in MySQL 3.x; it might be a database-dependent type of attribute. I don't imagine so, especially since the CPAN POD doesn't hint towards this, however I have noticed fishy things with earlier releases of MySQL in very specific curcumstances, and it may be worth looking into if nothing else.

    Good Luck!!

    ---hA||ta----
    print map{$_.' '}grep{/\w+/}@{[reverse(qw{Perl Code})]} or die while ( 'trying' );