From 934db2efb623f25c953415f7c591e66cb92c0425 Mon Sep 17 00:00:00 2001 From: Vladislav Vaintroub Date: Fri, 24 Nov 2023 18:48:27 +0100 Subject: [PATCH] MDEV-32875 SERVER_STATUS_AUTOCOMMIT set after connecting, if autocommit=0 After successful connection, server always sets SERVER_STATUS_AUTOCOMMIT in server_status in the OK packet. This is wrong, if global variable autocommit=0. Fixed THD::init(), added mysql_client_test test. Thanks to Diego Dupin for the providing the patch. Signed-off-by: Vladislav Vaintroub --- sql/sql_class.cc | 4 +++- tests/mysql_client_test.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/sql/sql_class.cc b/sql/sql_class.cc index bee793b7d0f..184aa731f3a 100644 --- a/sql/sql_class.cc +++ b/sql/sql_class.cc @@ -1227,7 +1227,9 @@ void THD::init() user_time.val= start_time= start_time_sec_part= 0; - server_status= SERVER_STATUS_AUTOCOMMIT; + server_status= 0; + if (variables.option_bits & OPTION_AUTOCOMMIT) + server_status|= SERVER_STATUS_AUTOCOMMIT; if (variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) server_status|= SERVER_STATUS_NO_BACKSLASH_ESCAPES; if (variables.sql_mode & MODE_ANSI_QUOTES) diff --git a/tests/mysql_client_test.c b/tests/mysql_client_test.c index a5178837f97..da04b078e76 100644 --- a/tests/mysql_client_test.c +++ b/tests/mysql_client_test.c @@ -21770,6 +21770,44 @@ static void test_mdev_30159() myquery(rc); } +/* + Check that server_status returned after connecting to server + is consistent with the value of autocommit variable. +*/ +static void test_connect_autocommit() +{ + int rc; + my_bool autocommit[]= {0, 1}; + int i; + rc= mysql_query(mysql, "SET @save_autocommit=@@global.autocommit"); + myquery(rc); + for (i= 0; i < 2; i++) + { + MYSQL *con; + char query[100]; + int autocommit_val; + + con= mysql_client_init(NULL); + DIE_UNLESS(con); + autocommit_val = autocommit[i]; + snprintf(query, sizeof(query), "SET global autocommit=%d", autocommit_val); + rc= mysql_query(mysql, query); + myquery(rc); + + if (!(mysql_real_connect(con, opt_host, opt_user, opt_password, current_db, + opt_port, opt_unix_socket, 0))) + { + fprintf(stderr, "Failed to connect to database: Error: %s\n", + mysql_error(con)); + exit(1); + } + DIE_UNLESS(!!(con->server_status & SERVER_STATUS_AUTOCOMMIT) == autocommit_val); + mysql_close(con); + } + rc= mysql_query(mysql, "SET global autocommit=@save_autocommit"); + myquery(rc); +} + static struct my_tests_st my_tests[]= { { "test_mdev_20516", test_mdev_20516 }, { "test_mdev24827", test_mdev24827 }, @@ -22074,6 +22112,7 @@ static struct my_tests_st my_tests[]= { { "test_mdev18408", test_mdev18408 }, { "test_mdev20261", test_mdev20261 }, { "test_mdev_30159", test_mdev_30159 }, + { "test_connect_autocommit", test_connect_autocommit}, { 0, 0 } };