Mysql c++连接器,如何保持连接存活和重新连接,如果连接断开
mysql c++ connector, how to keep the connection alive and reconnect if connection is broked?
我正在使用mysql c++连接器。我想连接到我的数据库,并保持连接,如果连接断开,重新连接。
这里有一个连接代码:
driver = sql::mysql::get_driver_instance();
connection = driver->connect(Server, UserID, Password);
这里的表示方法connection->isValid()和reconnect()用于了解连接是否处于活动状态,如果不是则重新连接。所以我要这样做:
bool MySqlCommunicator::Connect()
{
try
{
bool connected = connection != NULL && (connection->isValid() || connection->reconnect());
if (!connected)
{
connection = driver->connect(Server, UserID, Password);
connected = connection->isValid();
}
if (connected)
{
statement = connection->createStatement();
char str[255];
sprintf(str, "USE %s", Database);
statement->execute(str);
return true;
}
return false;
}
catch (sql::SQLException &e)
{
delete connection;
connection = NULL;
return false;
}
}
然后每次我想执行查询时,我调用Connect()以确保连接就绪。我假设Connect()方法将在连接不存在的情况下重新连接。
但是当到达这一行时程序崩溃了:
bool connected = connection != NULL && (connection->isValid() || connection->reconnect());
无法执行isValid()方法,程序退出,消息分段错误。
好吧,我把代码改成如下:
bool MySqlCommunicator::Connect()
{
try
{
bool connected = connection != NULL; //connection != NULL && (connection->isValid() || connection->reconnect());
if (!connected)
{
connection = driver->connect(Server, UserID, Password);
//connected = connection->isValid();
connected = true;
}
if (connected)
{
statement = connection->createStatement();
char str[255];
sprintf(str, "USE %s", Database);
statement->execute(str);
return true;
}
return false;
}
catch (sql::SQLException &e)
{
delete connection;
connection = NULL;
return false;
}
}
现在它工作了!如果发生任何错误,它将重新连接以使其正确。但这不是一个解决方案!我想要一个合适的解决办法。
- 为什么我的第一个代码在上面提到的行上崩溃了?其他方法如connection->setReadOnly()也会使程序崩溃!它的原因是什么?
- 确保连接是活的最好方法是什么?
- 在发生错误时重新连接到远程数据库的最佳实践是什么?
等着有人来回答!提前感谢)
经过几个小时的挖掘,我找到了解决方案。我交叉编译了新版本的mysql c++连接器,并使用它来编译和运行我的代码。现在方法isValid()和reconnect()正在工作。(setreadOnly()方法似乎还没有实现)。
和我的最终工作代码是:
bool MySqlCommunicator::Connect()
{
try
{
bool connected = connection != NULL && (connection->isValid() || connection->reconnect());
if (!connected)
{
connection = driver->connect(Server, UserID, Password);
connected = connection->isValid();
}
if (connected)
{
//connection->setReadOnly(false);
statement = connection->createStatement();
char str[255];
sprintf(str, "USE %s", Database);
statement->execute(str);
return true;
}
return false;
}
catch (sql::SQLException &e)
{
delete connection;
connection = NULL;
return false;
}
}
相关文章:
- QTcpSocket在不阻塞GUI的情况下重新连接到服务器
- QTcpSocket 在 RemoteHostClosedError 后重新连接到服务器时无法传输数据
- 使用 ssh 重新连接到远程 Ubuntu 机器后,如何继续使用 gdb 调试 c++ 代码?
- 客户端 gRPC 连接,C++重新连接?
- 以编程方式重新连接断开的 VHD 链
- 断开连接后重新连接boost beast(asio)websocket和http连接时出错
- boost1.62 在 docker 容器中重新连接后套接字损坏
- 尝试重新连接到服务器时获取错误提升 asio 连接超时
- 将现有套接字重新连接到新服务器所需的最少工作量是多少
- OPENCV如何重新连接中间具有空白或曲率的线路
- 我可以在不重新连接的情况下多次使用BIO_puts、BIO_read吗
- QTcpSocket客户端自动重新连接
- QTcpSocket重新连接方式
- Winsock-正在将客户端重新连接到服务器-10061
- OpenCV VideoCapture IP摄像机重新连接
- 无法重新连接boost asio套接字客户端
- Mysql c++连接器,如何保持连接存活和重新连接,如果连接断开
- 套接字将在一段时间后死亡而不能重新连接
- 服务器关闭后TCP/IP客户端重新连接
- Winsock;在重新连接之前,我需要关闭插座吗