diff --git a/Data/MySQL/src/StatementExecutor.cpp b/Data/MySQL/src/StatementExecutor.cpp index ad8290797..3f3857d84 100644 --- a/Data/MySQL/src/StatementExecutor.cpp +++ b/Data/MySQL/src/StatementExecutor.cpp @@ -55,8 +55,17 @@ void StatementExecutor::prepare(const std::string& query) return; } - if (mysql_stmt_prepare(_pHandle, query.c_str(), static_cast(query.length())) != 0) - throw StatementException("mysql_stmt_prepare error", _pHandle, query); + int rc = mysql_stmt_prepare(_pHandle, query.c_str(), static_cast(query.length())); + if (rc != 0) + { + // retry if connection lost + int err = mysql_errno(_pSessionHandle); + if (err == 2006 /* CR_SERVER_GONE_ERROR */ || err == 2013 /* CR_SERVER_LOST */) + { + rc = mysql_stmt_prepare(_pHandle, query.c_str(), static_cast(query.length())); + } + } + if (rc != 0) throw StatementException("mysql_stmt_prepare error", _pHandle, query); _query = query; _state = STMT_COMPILED;