Skip to content

Commit 650fd80

Browse files
palichoroba
authored andcommitted
Do not call mariadb_db_reconnect() during changing $dbh attributes when not needed
Currently mariadb_db_STORE_attrib() function (handler for changing dbh attributes via $dbh->{...} = ... syntax) at its beginning calls function mariadb_db_reconnect() to ensure that imp_dbh->pmysql connection structure is initialized. Change logic of mariadb_db_STORE_attrib() function to call function mariadb_db_reconnect() only when the changing of attribute really requires active connection. This change fixes a problem that Perl for locatized variables is trying to restore its original value after leaving the scope, and this is failing for restoring the DBI attributes. Trying to change common DBI attributes (those which first letter is upper case) except the AutoConnect, is not handled by DBD::MariaDB driver, and therefore DBD::MariaDB driver does not have to check if the connection is still active.
1 parent 78f031b commit 650fd80

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

dbdimp.c

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3402,14 +3402,6 @@ mariadb_db_STORE_attrib(
34023402
char *key = SvPV(keysv, kl); /* needs to process get magic */
34033403
const bool bool_value = SvTRUE_nomg(valuesv);
34043404

3405-
if (!imp_dbh->pmysql && !mariadb_db_reconnect(dbh, NULL))
3406-
{
3407-
mariadb_dr_do_error(dbh, CR_SERVER_GONE_ERROR, "MySQL server has gone away", "HY000");
3408-
if (memEQs(key, kl, "AutoCommit"))
3409-
croak_autocommit_failure(); /* does not return */
3410-
return 0;
3411-
}
3412-
34133405
if (memEQs(key, kl, "AutoCommit"))
34143406
{
34153407
bool oldval = DBIc_has(imp_dbh, DBIcf_AutoCommit) ? TRUE : FALSE;
@@ -3420,6 +3412,11 @@ mariadb_db_STORE_attrib(
34203412
/* if setting AutoCommit on ... */
34213413
if (!imp_dbh->no_autocommit_cmd)
34223414
{
3415+
if (!imp_dbh->pmysql && !mariadb_db_reconnect(dbh, NULL))
3416+
{
3417+
mariadb_dr_do_error(dbh, CR_SERVER_GONE_ERROR, "MySQL server has gone away", "HY000");
3418+
croak_autocommit_failure(); /* does not return */
3419+
}
34233420
if (
34243421
mysql_autocommit(imp_dbh->pmysql, bool_value)
34253422
)
@@ -3470,10 +3467,20 @@ mariadb_db_STORE_attrib(
34703467
error_nul_character(dbh, "mariadb_fabric_opt_group");
34713468
return 0;
34723469
}
3470+
if (!imp_dbh->pmysql && !mariadb_db_reconnect(dbh, NULL))
3471+
{
3472+
mariadb_dr_do_error(dbh, CR_SERVER_GONE_ERROR, "MySQL server has gone away", "HY000");
3473+
return 0;
3474+
}
34733475
mysql_options(imp_dbh->pmysql, FABRIC_OPT_GROUP, str);
34743476
}
34753477
else if (memEQs(key, kl, "mariadb_fabric_opt_default_mode"))
34763478
{
3479+
if (!imp_dbh->pmysql && !mariadb_db_reconnect(dbh, NULL))
3480+
{
3481+
mariadb_dr_do_error(dbh, CR_SERVER_GONE_ERROR, "MySQL server has gone away", "HY000");
3482+
return 0;
3483+
}
34773484
if (SvOK(valuesv)) {
34783485
STRLEN len;
34793486
const char *str = SvPV_nomg(valuesv, len);
@@ -3498,6 +3505,11 @@ mariadb_db_STORE_attrib(
34983505
mariadb_dr_do_error(dbh, CR_UNKNOWN_ERROR, "Valid settings for FABRIC_OPT_MODE are 'ro' or 'rw'", "HY000");
34993506
return 0;
35003507
}
3508+
if (!imp_dbh->pmysql && !mariadb_db_reconnect(dbh, NULL))
3509+
{
3510+
mariadb_dr_do_error(dbh, CR_SERVER_GONE_ERROR, "MySQL server has gone away", "HY000");
3511+
return 0;
3512+
}
35013513
mysql_options(imp_dbh->pmysql, FABRIC_OPT_MODE, str);
35023514
}
35033515
else if (memEQs(key, kl, "mariadb_fabric_opt_group_credentials"))
@@ -3518,6 +3530,11 @@ mariadb_db_STORE_attrib(
35183530
/* MYSQL_OPT_MAX_ALLOWED_PACKET was added in MariaDB 10.2.6 and MariaDB 10.3.1 */
35193531
UV uv = SvUV_nomg(valuesv);
35203532
unsigned long packet_size = (uv <= ULONG_MAX ? uv : ULONG_MAX);
3533+
if (!imp_dbh->pmysql && !mariadb_db_reconnect(dbh, NULL))
3534+
{
3535+
mariadb_dr_do_error(dbh, CR_SERVER_GONE_ERROR, "MySQL server has gone away", "HY000");
3536+
return 0;
3537+
}
35213538
mysql_options(imp_dbh->pmysql, MYSQL_OPT_MAX_ALLOWED_PACKET, &packet_size);
35223539
#else
35233540
/* before MySQL 5.7.9 and MariaDB 10.2.6 and MariaDB 10.3.0 it is not possible to change max_allowed_packet after connection was established */
@@ -3530,6 +3547,11 @@ mariadb_db_STORE_attrib(
35303547
{
35313548
if (!imp_dbh->connected) /* When not connected, it is handled in mariadb_dr_connect() */
35323549
return 0;
3550+
if (!imp_dbh->pmysql && !mariadb_db_reconnect(dbh, NULL))
3551+
{
3552+
mariadb_dr_do_error(dbh, CR_SERVER_GONE_ERROR, "MySQL server has gone away", "HY000");
3553+
return 0;
3554+
}
35333555
if (mysql_set_server_option(imp_dbh->pmysql, bool_value ? MYSQL_OPTION_MULTI_STATEMENTS_ON : MYSQL_OPTION_MULTI_STATEMENTS_OFF) != 0)
35343556
{
35353557
mariadb_dr_do_error(dbh, mysql_errno(imp_dbh->pmysql), mysql_error(imp_dbh->pmysql), mysql_sqlstate(imp_dbh->pmysql));

0 commit comments

Comments
 (0)