Skip to content

Commit

Permalink
Enable auto reconnect for ER_CLIENT_INTERACTION_TIMEOUT
Browse files Browse the repository at this point in the history
  • Loading branch information
dveeden committed Nov 3, 2023
1 parent 6db0224 commit b5d72db
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
12 changes: 11 additions & 1 deletion dbdimp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4438,6 +4438,9 @@ int mysql_db_reconnect(SV* h)
imp_dbh_t* imp_dbh;
MYSQL save_socket;

if (DBIc_DBISTATE(imp_xxh)->debug >= 2)
PerlIO_printf(DBIc_LOGPIO(imp_xxh), "reconnecting\n");

if (DBIc_TYPE(imp_xxh) == DBIt_ST)
{
imp_dbh = (imp_dbh_t*) DBIc_PARENT_COM(imp_xxh);
Expand All @@ -4457,16 +4460,23 @@ int mysql_db_reconnect(SV* h)
}

if (mysql_errno(imp_dbh->pmysql) != CR_SERVER_GONE_ERROR &&
mysql_errno(imp_dbh->pmysql) != CR_SERVER_LOST)
mysql_errno(imp_dbh->pmysql) != CR_SERVER_LOST &&
mysql_errno(imp_dbh->pmysql) != ER_CLIENT_INTERACTION_TIMEOUT) {
/* Other error */
if (DBIc_DBISTATE(imp_xxh)->debug >= 2)
PerlIO_printf(DBIc_LOGPIO(imp_xxh), "Can't reconnect on unexpected error %d\n",
mysql_errno(imp_dbh->pmysql));
return FALSE;
}

if (!DBIc_has(imp_dbh, DBIcf_AutoCommit) || !imp_dbh->auto_reconnect)
{
/* We never reconnect if AutoCommit is turned off.
* Otherwise we might get an inconsistent transaction
* state.
*/
if (DBIc_DBISTATE(imp_xxh)->debug >= 2)
PerlIO_printf(DBIc_LOGPIO(imp_xxh), "Can't reconnect as AutoCommit is turned off\n");
return FALSE;
}

Expand Down
7 changes: 6 additions & 1 deletion t/15reconnect.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ eval {$dbh = DBI->connect($test_dsn, $test_user, $test_password,
if ($@) {
plan skip_all => "no database connection";
}
plan tests => 28;
plan tests => 34;

for my $mysql_server_prepare (0, 1) {
$dbh= DBI->connect("$test_dsn;mysql_server_prepare=$mysql_server_prepare;mysql_server_prepare_disable_fallback=1", $test_user, $test_password,
Expand All @@ -30,6 +30,11 @@ ok($dbh->{mysql_auto_reconnect} = 1, "enabling reconnect");

ok($dbh->{AutoCommit} = 1, "enabling autocommit");

ok ($dbh->do("SET SESSION wait_timeout=2"));
sleep(3);
ok($dbh->do("SELECT 1"), "implicitly reconnecting handle with 'do'");
ok($dbh->{Active}, "checking for reactivated handle");

ok($dbh->disconnect(), "disconnecting active handle");

ok(!$dbh->{Active}, "checking for inactive handle");
Expand Down

0 comments on commit b5d72db

Please sign in to comment.