gpt4 book ai didi

c++ - MySQL 异常 "connection lost during query"、 "MySQL server has gone away"和 "command out of sync"

转载 作者:行者123 更新时间:2023-11-29 18:04:33 26 4
gpt4 key购买 nike

我有一个 C++ 代码,它使用 SQLAPI++ 库。在我的代码中,我尝试打开 mysql 连接,然后创建两个使用相同连接的命令,并从两个线程调用该命令。在执行代码期间,我收到异常“查询期间连接丢失”和“MySQL 服务器已消失”。有时我收到异常“命令不同步,您现在无法运行此命令”。我尝试增加wait_timeout 150和max_allowed_pa​​ckage 64M,但我的问题没有解决。下面你可以找到 C++ 代码。

int main()
{

try
{
SAConnection saConnection;
saConnection.Connect("scada", "root", "123qwerty!@#", SA_MySQL_Client);
saConnection.setAutoCommit(SA_AutoCommitOff);

SACommand saCommand1{&saConnection, "select * from Node"};
SACommand saCommand2{&saConnection, "select * from Node limit 1"};

// VM: TODO: saCommand1.setOption("UseStatement") = "TRUE";
// VM: TODO: saCommand2.setOption("UseStatement") = "TRUE";
saCommand1.setOption("HandleResult") = "store";
saCommand2.setOption("HandleResult") = "store";

auto f1 = std::async(std::launch::async, [&]{ saCommand1.Execute(); saConnection.Commit(); });
auto f2 = std::async(std::launch::async, [&]{ saCommand2.Execute(); saConnection.Commit(); });
f1.get();
f2.get();

//saConnection.Commit(); // BOOM!

/*
while (saCommand1.FetchNext())
std::cerr << "======== fetching a record from saCommand1" << std::endl;
while (saCommand2.FetchNext())
std::cerr << "======== fetching a record from saCommand2" << std::endl;
*/
}
catch(const SAException& ex)
{
std::cerr << "==== ex.what() is " << static_cast<const char*>(ex.ErrText()) << std::endl;
}

return 0;
}

最佳答案

您正在使用来自两个 std::async 对象的一个​​连接,可能来自两个线程,这是不受支持的。创建两个到数据库的连接,每个 std::async 一个连接。

关于c++ - MySQL 异常 "connection lost during query"、 "MySQL server has gone away"和 "command out of sync",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48008789/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com