当数据库突然断开的时候,API将接收不到MySQL的连接,在代码里面写关于自动重连可以实现数据库断开之后重连。
//mysql参数的设定 设置自动重连 在connect之前调用
/*
int STDCALL mysql_options(MYSQL *mysql, enum mysql_option option,
const void *arg);
*/
bool Option(LX_option opt, const void* arg);
在传入参数的时候,我们知道,关于在代码部分输入 mysql_options(),按住键盘ctrl,鼠标点击进入定义,我们可以看到关于mysql_options()的构造。
第一个就是mysql数据库,第二个是对应的mysql_option的枚举,第三个是一个对应的配置值。
int STDCALL mysql_options(MYSQL *mysql, enum mysql_option option,
const void *arg);
enum mysql_option的构造如下
enum mysql_option {
MYSQL_OPT_CONNECT_TIMEOUT,
MYSQL_OPT_COMPRESS,
MYSQL_OPT_NAMED_PIPE,
MYSQL_INIT_COMMAND,
MYSQL_READ_DEFAULT_FILE,
MYSQL_READ_DEFAULT_GROUP,
MYSQL_SET_CHARSET_DIR,
MYSQL_SET_CHARSET_NAME,
MYSQL_OPT_LOCAL_INFILE,
MYSQL_OPT_PROTOCOL,
MYSQL_SHARED_MEMORY_BASE_NAME,
MYSQL_OPT_READ_TIMEOUT,
MYSQL_OPT_WRITE_TIMEOUT,
MYSQL_OPT_USE_RESULT,
MYSQL_REPORT_DATA_TRUNCATION,
MYSQL_OPT_RECONNECT,
MYSQL_PLUGIN_DIR,
MYSQL_DEFAULT_AUTH,
MYSQL_OPT_BIND,
MYSQL_OPT_SSL_KEY,
MYSQL_OPT_SSL_CERT,
MYSQL_OPT_SSL_CA,
MYSQL_OPT_SSL_CAPATH,
MYSQL_OPT_SSL_CIPHER,
MYSQL_OPT_SSL_CRL,
MYSQL_OPT_SSL_CRLPATH,
MYSQL_OPT_CONNECT_ATTR_RESET,
MYSQL_OPT_CONNECT_ATTR_ADD,
MYSQL_OPT_CONNECT_ATTR_DELETE,
MYSQL_SERVER_PUBLIC_KEY,
MYSQL_ENABLE_CLEARTEXT_PLUGIN,
MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS,
MYSQL_OPT_MAX_ALLOWED_PACKET,
MYSQL_OPT_NET_BUFFER_LENGTH,
MYSQL_OPT_TLS_VERSION,
MYSQL_OPT_SSL_MODE,
MYSQL_OPT_GET_SERVER_PUBLIC_KEY,
MYSQL_OPT_RETRY_COUNT,
MYSQL_OPT_OPTIONAL_RESULTSET_METADATA,
MYSQL_OPT_SSL_FIPS_MODE,
MYSQL_OPT_TLS_CIPHERSUITES,
MYSQL_OPT_COMPRESSION_ALGORITHMS,
MYSQL_OPT_ZSTD_COMPRESSION_LEVEL,
MYSQL_OPT_LOAD_DATA_LOCAL_DIR,
MYSQL_OPT_USER_PASSWORD,
MYSQL_OPT_SSL_SESSION_DATA
};
enum LX_option {
LX_OPT_CONNECT_TIMEOUT,
LX_OPT_COMPRESS,
LX_OPT_NAMED_PIPE,
LX_INIT_COMMAND,
LX_READ_DEFAULT_FILE,
LX_READ_DEFAULT_GROUP,
LX_SET_CHARSET_DIR,
LX_SET_CHARSET_NAME,
LX_OPT_LOCAL_INFILE,
LX_OPT_PROTOCOL,
LX_SHARED_MEMORY_BASE_NAME,
LX_OPT_READ_TIMEOUT,
LX_OPT_WRITE_TIMEOUT,
LX_OPT_USE_RESULT,
LX_REPORT_DATA_TRUNCATION,
LX_OPT_RECONNECT,
LX_PLUGIN_DIR,
LX_DEFAULT_AUTH,
LX_OPT_BIND,
LX_OPT_SSL_KEY,
LX_OPT_SSL_CERT,
LX_OPT_SSL_CA,
LX_OPT_SSL_CAPATH,
LX_OPT_SSL_CIPHER,
LX_OPT_SSL_CRL,
LX_OPT_SSL_CRLPATH,
LX_OPT_CONNECT_ATTR_RESET,
LX_OPT_CONNECT_ATTR_ADD,
LX_OPT_CONNECT_ATTR_DELETE,
LX_SERVER_PUBLIC_KEY,
LX_ENABLE_CLEARTEXT_PLUGIN,
LX_OPT_CAN_HANDLE_EXPIRED_PASSWORDS,
LX_OPT_MAX_ALLOWED_PACKET,
LX_OPT_NET_BUFFER_LENGTH,
LX_OPT_TLS_VERSION,
LX_OPT_SSL_MODE,
LX_OPT_GET_SERVER_PUBLIC_KEY,
LX_OPT_RETRY_COUNT,
LX_OPT_OPTIONAL_RESULTSET_METADATA,
LX_OPT_SSL_FIPS_MODE,
LX_OPT_TLS_CIPHERSUITES,
LX_OPT_COMPRESSION_ALGORITHMS,
LX_OPT_ZSTD_COMPRESSION_LEVEL,
LX_OPT_LOAD_DATA_LOCAL_DIR,
LX_OPT_USER_PASSWORD,
LX_OPT_SSL_SESSION_DATA
};
#include"LXData.h"
bool LXMysql::Option(LX_option opt, const void* arg)
{
if (!mysql)//如果mysql没有初始化好
{
cerr << "Option failed: mysql is NULL" << endl;
return false;
}
//(mysql_option)opt 强转opt
int re = mysql_options(mysql, (mysql_option)opt, arg);
if (re != 0)
{
cerr << "mysql_options failed!" << mysql_error(mysql) << endl;
return false;
}
return true;
}
//连接超时时间
bool SetConnectTimeout(int sec);
//自动重连,默认不自动
bool SetReConnect(bool isre = true);
bool LXMysql::SetConnectTimeout(int sec)
{
return Option(LX_OPT_CONNECT_TIMEOUT, &sec);
}
bool LXMysql::SetReConnect(bool isre)
{
return Option(LX_OPT_RECONNECT, &isre);
}
//连接超时秒
my.SetConnectTimeout(3);
//自动重连
my.SetReConnect(true);
// 测试自动重连是否生效
for (;;)
{
//flush 刷新一下
cout << my.Query(sql.c_str()) << flush;
}