Skip to content

Commit 497f15d

Browse files
committed
Bug#36316146 JDBC: opt_reconnect option is not working as expected
Fixed by merging changes of WL#15979.
2 parents 6aa073a + 9733274 commit 497f15d

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

jdbc/driver/nativeapi/mysql_native_connection_wrapper.cpp

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,16 @@ get_mysql_option(sql::mysql::MySQL_Connection_Options opt)
7676
case sql::mysql::MYSQL_OPT_WRITE_TIMEOUT: return ::MYSQL_OPT_WRITE_TIMEOUT;
7777
case sql::mysql::MYSQL_OPT_USE_RESULT: return ::MYSQL_OPT_USE_RESULT;
7878
case sql::mysql::MYSQL_REPORT_DATA_TRUNCATION: return ::MYSQL_REPORT_DATA_TRUNCATION;
79-
case sql::mysql::MYSQL_OPT_RECONNECT: return ::MYSQL_OPT_RECONNECT;
79+
case sql::mysql::MYSQL_OPT_RECONNECT:
80+
#if MYCPPCONN_STATIC_MYSQL_VERSION_ID < 80300
81+
return ::MYSQL_OPT_RECONNECT;
82+
#else
83+
{
84+
std::string errorOption("MYSQL_OPT_RECONNECT");
85+
throw sql::SQLUnsupportedOptionException("Option is not supported",
86+
errorOption);
87+
}
88+
#endif
8089
case sql::mysql::MYSQL_PLUGIN_DIR: return ::MYSQL_PLUGIN_DIR;
8190
case sql::mysql::MYSQL_DEFAULT_AUTH: return ::MYSQL_DEFAULT_AUTH;
8291
case sql::mysql::MYSQL_OPT_BIND: return ::MYSQL_OPT_BIND;
@@ -177,6 +186,15 @@ MySQL_NativeConnectionWrapper::connect(const ::sql::SQLString & host,
177186
const ::sql::SQLString & socket_or_pipe,
178187
unsigned long client_flag)
179188
{
189+
m_host = host;
190+
m_user = user;
191+
m_passwd = passwd;
192+
m_db = db;
193+
m_port = port;
194+
m_socket_or_pipe = socket_or_pipe;
195+
m_client_flag = client_flag;
196+
m_dns_srv = false;
197+
180198
return (NULL != api->real_connect(mysql, nullIfEmpty(host), user.c_str(),
181199
nullIfEmpty(passwd),
182200
nullIfEmpty(db), port,
@@ -193,6 +211,13 @@ MySQL_NativeConnectionWrapper::connect_dns_srv(const ::sql::SQLString & host,
193211
const ::sql::SQLString & db,
194212
unsigned long client_flag)
195213
{
214+
m_host = host;
215+
m_user = user;
216+
m_passwd = passwd;
217+
m_db = db;
218+
m_client_flag = client_flag;
219+
m_dns_srv = true;
220+
196221
return (NULL != api->real_connect_dns_srv(mysql, nullIfEmpty(host), user.c_str(),
197222
nullIfEmpty(passwd),
198223
nullIfEmpty(db), client_flag));
@@ -318,6 +343,13 @@ MySQL_NativeConnectionWrapper::next_result()
318343
int
319344
MySQL_NativeConnectionWrapper::options(::sql::mysql::MySQL_Connection_Options option, const void * value)
320345
{
346+
#if MYCPPCONN_STATIC_MYSQL_VERSION_ID >= 80300
347+
if (option == MYSQL_OPT_RECONNECT) {
348+
reconnect = *(bool*)value;
349+
// For reconnect option we don't pass the call to api.
350+
return 0;
351+
}
352+
#endif
321353
return api->options(mysql, get_mysql_option(option), value);
322354
}
323355
/* }}} */
@@ -378,6 +410,14 @@ MySQL_NativeConnectionWrapper::options(::sql::mysql::MySQL_Connection_Options op
378410
int
379411
MySQL_NativeConnectionWrapper::get_option(::sql::mysql::MySQL_Connection_Options option, const void * value)
380412
{
413+
#if MYCPPCONN_STATIC_MYSQL_VERSION_ID >= 80300
414+
if (option == MYSQL_OPT_RECONNECT) {
415+
*(bool*)value = reconnect;
416+
// For reconnect option we don't pass the call to api.
417+
return 0;
418+
}
419+
#endif
420+
381421
return api->get_option(mysql, get_mysql_option(option), value);
382422
}
383423
/* }}} */
@@ -506,7 +546,25 @@ MySQL_NativeConnectionWrapper::query(const SQLString & stmt_str)
506546
int
507547
MySQL_NativeConnectionWrapper::ping()
508548
{
509-
return api->ping(mysql);
549+
int res = api->ping(mysql);
550+
551+
#if MYCPPCONN_STATIC_MYSQL_VERSION_ID >= 80300
552+
if (res && reconnect) {
553+
// Try reconnecting if could not ping and reconnect
554+
// option is set.
555+
bool connect_result = !m_dns_srv ?
556+
connect(m_host, m_user, m_passwd, m_db, m_port,
557+
m_socket_or_pipe, m_client_flag)
558+
:
559+
connect_dns_srv(m_host, m_user, m_passwd, m_db,
560+
m_client_flag);
561+
// If connected return success, otherwise let ping()
562+
// return the proper error.
563+
res = connect_result ? 0 : api->ping(mysql);
564+
}
565+
#endif
566+
567+
return res;
510568
}
511569
/* }}} */
512570

jdbc/driver/nativeapi/mysql_native_connection_wrapper.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ class MySQL_NativeConnectionWrapper : public NativeConnectionWrapper
5959
/* api should be declared before mysql here */
6060
std::shared_ptr<IMySQLCAPI> api;
6161

62+
bool reconnect = false;
63+
::sql::SQLString m_host;
64+
::sql::SQLString m_user;
65+
::sql::SQLString m_passwd;
66+
::sql::SQLString m_db;
67+
unsigned int m_port;
68+
::sql::SQLString m_socket_or_pipe;
69+
unsigned long m_client_flag;
70+
bool m_dns_srv = false;
71+
6272
#if (MYCPPCONN_STATIC_MYSQL_VERSION_ID > 80004)
6373
struct MYSQL* mysql;
6474
#else

0 commit comments

Comments
 (0)