今天在项目中使用了Mysql++调用Mysql的存储过程来实现功能,但是碰到一个有意思的问题,当成功调用存储过程后,再次做其他的查询,确返回Commands out of sync; you can't run this command now的错误。

        Query query = conn.query();memset(cSql, 0, sizeof(cSql));sprintf(cSql, "call %s('%s','0','1',@phoneno,@uuid,@projectid,@batchid,@companyid,@displaynum,@dialtype)", m_AutoPrcoName, strChan.GetBuffer(0));StoreQueryResult pResult = query.store(cSql, strlen(cSql));if (!pResult){CallBack_LogReport(Input->nChan, 3, "AutoDialout_MultiCom error1\n");time(&m_ChanInfo[Input->nChan].LastNoDialoutTime);pthread_mutex_unlock(&m_DialoutMutex);return -1;}sprintf(cSql, "select @phoneno,@uuid,@projectid,@batchid,@companyid,@displaynum,@dialtype");pResult = query.store(cSql, strlen(cSql)); //此处出现问题if (!pResult){CallBack_LogReport(Input->nChan, 3, "AutoDialout_MultiCom error2\n");time(&m_ChanInfo[Input->nChan].LastNoDialoutTime);pthread_mutex_unlock(&m_DialoutMutex);return -1;}

查阅了官方的文档,其实对这个问题的描述还是很清晰的,而且专门用了一节,也给出了解决方案,现摘录如下(只有英文哦),其实关于就是在于对于返回多个数据集结果的查询,必须讲数据集结果取完,即使并没有什么用,否则就会报出Commands out of sync; you can't run this command now的错误。

3.16. Concurrent Queries on a Connection
An important limitation of the MySQL C API library — which MySQL++ is built atop, so it shares this limitation —
is that you can only have one query in progress on each connection to the database server. If you try to issue a second
query while one is still in progress, you get an obscure error message about “Commands out of sync” from the underlying
C API library. (You normally get this message in a MySQL++ exception unless you have exceptions disabled, in which
case you get a failure code and Connection::error() returns this message.)
There are lots of ways to run into this limitation:
• The easiest way is to try to use a single Connection object in a multithreaded program, with more than one thread
attempting to use it to issue queries. Unless you put in a lot of work to synchronize access, this is almost guaranteed
to fail at some point, giving the dread “Commands out of sync” error.
• You might then think to give each thread that issues queries its own Connection object. You can still run into
trouble if you pass the data you get from queries around to other threads. What can happen is that one of these child
objects indirectly calls back to the Connection at a time where it’s involved with another query. This is properly
covered elsewhere, in Section 7.4, “Sharing MySQL++ Data Structures”.)
• One way to run into this problem without using threads is with “use” queries, discussed above. If you don’t consume
all rows from a query before you issue another on that connection, you are effectively trying to have multiple
concurrent queries on a single connection. Here’s a recipie for this particular disaster:
UseQueryResult r1 = query.use("select garbage from plink where foobie='tamagotchi'");
UseQueryResult r2 = query.use("select blah from bonk where bletch='smurf'");
The second use() call fails because the first result set hasn’t been consumed yet.
• Still another way to run into this limitation is if you use MySQL’s multi-query feature. This lets you give multiple
queries in a single call, separated by semicolons, and get back the results for each query separately. If you issue
three queries using Query::store(), you only get back the first query’s results with that call, and then have to
call store_next() to get the subsequent query results. MySQL++ provides Query::more_results() so
you know whether you’re done, or need to call store_next() again. Until you reach the last result set, you can’t
issue another query on that connection.
• Finally, there’s a way to run into this that surprises almost everyone sooner or later: stored procedures. MySQL
normally returns at least two result sets for a stored procedure call. The simple case is that the stored procedure
contains a single SQL query, and it succeeds: you get two results, first the results of the embedded SQL query, and
then the result of the call itself. If there are multiple SQL queries within the stored procedure, you get more than
two result sets. Until you consume them all, you can’t start a new query on the connection. As above, you want to
have a loop calling more_results() and store_next() to work your way through all of the result sets
produced by the stored procedure call.

按照文档的提示,修改代码如下:

        Query query = conn.query();memset(cSql, 0, sizeof(cSql));sprintf(cSql, "call %s('%s','0','1',@phoneno,@uuid,@projectid,@batchid,@companyid,@displaynum,@dialtype)", m_AutoPrcoName, strChan.GetBuffer(0));StoreQueryResult pResult = query.store(cSql, strlen(cSql));if (!pResult){CallBack_LogReport(Input->nChan, 3, "AutoDialout_MultiCom error1\n");time(&m_ChanInfo[Input->nChan].LastNoDialoutTime);pthread_mutex_unlock(&m_DialoutMutex);return -1;}for (int i = 1; query.more_results(); ++i){query.store_next(); //必须取完结果}sprintf(cSql, "select @phoneno,@uuid,@projectid,@batchid,@companyid,@displaynum,@dialtype");pResult = query.store(cSql, strlen(cSql));if (!pResult){CallBack_LogReport(Input->nChan, 3, "AutoDialout_MultiCom error2\n");time(&m_ChanInfo[Input->nChan].LastNoDialoutTime);pthread_mutex_unlock(&m_DialoutMutex);return -1;}

好了,问题解决!

Mysql++关于多数据集查询Commands out of sync; you can‘t run this command now的问题相关推荐

  1. mysql error:2014 Commands out of sync; you can't run this command now

    最近在项目中经常会出现数据库操作失败的,日志抛出的错误的 Commands out of sync; you can't run this command now.但是把具体的这条sql语句放到可视化 ...

  2. commands out of sync mysql_MySQL问题一则:Commands out of sync; you can't run this command now以及相关问题...

    录制程序有一功能:将录制的文件信息写入MySQL数据库,供BS系统查询. 因此封装了一个MySQL类,进行数据库操作. 主要接口为Update():执行SQL语句. 现在问题来了: (一)在某个场景下 ...

  3. mysql执行存储过程提示out of_PHP执行MYSQL存储过程报错:Commands out of sync; you can't run...

    php中在同时执行2个存储过程时,有一个程序2个储存过程都执行,有一个程序只执行第一个调用.2个都执行的调用如下: $mydb->query("delete from pinfo wh ...

  4. commands out of sync mysql,MySQL-python: Commands out of sync

    在给 MySQL 数据库访问层增加新功能时遇到了这样的错误: ProgrammingError: (2014, "Commands out of sync; you can't run th ...

  5. commands out of sync错误

    第一种情况: commands out of sync.  Did you run multiple statements at once 可能的原因:是在事务之中写了查询语句. 解决方案:把查询语句 ...

  6. mysql如何提高其查询速度的方法

    2019独角兽企业重金招聘Python工程师标准>>> 最近一段时间由于工作需要,开始关注针对Mysql数据库的select查询语句的相关优化方法. 由于在参与的实际项目中发现当my ...

  7. Mysql高级-应用优化,查询缓存优化,锁

    文章目录 1. 应用优化 1.1 使用连接池 1.2 减少对MySQL的访问 1.2.1 避免对数据进行重复检索 1.2.2 增加cache层 1.3 负载均衡 1.3.1 利用MySQL复制分流查询 ...

  8. mysql表deptno,MySQL:多表查询

    MySQL:多表查询 SELECT查询不但可以从一张表查询数据,还可以从多张表同时查询数据.查询多张表的语法是:SELECT * FROM 表1 表2,普通多表查询会获取M x N行记录,所以一般使用 ...

  9. mysql对结果再查询_mysql 再查询结果的基础上查询(子查询)

    SELECT A.wx_name, A.wx_litpic, B . * FROM ( SELECT uid, COUNT( * ) AS daticishu FROM statements WHER ...

最新文章

  1. 任正非签发最新电邮:管理者的18种堕怠行为
  2. log4j:WARN No appenders could be found for logger ().解决方案
  3. 从零基础入门Tensorflow2.0 ----二、5.3 实战sklearn超参数搜索
  4. 聊聊 Tomcat 的单机多实例
  5. 三星java游戏下载_轻松游戏 三星Z500安装java游戏详细教程
  6. 嵌入式Linux学习笔记
  7. php如何获取手机序列号,Android应用获取设备序列号的方法
  8. android语音识别sdk接入收费吗,百度语音识别开放平台SDK使用方法
  9. 计算机无法进bios,del和F2进不了bios,详细教您电脑进不了bios怎么办
  10. 关于校外访问湖北经济学院校内图书馆资源的具体步骤
  11. dcos 1.7 目录挂载测试
  12. 《求职》第四部分 - 操作系统篇 - Linux基础
  13. html点按钮展开图片,案例:点击按钮隐藏图片 再次点击显示图片
  14. 2021-2027中国工业物联网通信产品市场现状及未来发展趋势
  15. STM32 -SPI关于nss引脚
  16. xp系统中提示文件WINDOWS\SYSTEM32\CONFIG\SYSTEM缺失问题的解决方法
  17. (HttpClient技术)(58同城系列)58同城发帖
  18. 刷屏的变脸软件,火一把就死?
  19. 计算机专业需要安装哪些软件,大学生电脑必装软件,快看你有哪些?
  20. 几款好用的免费内网穿透

热门文章

  1. 防火墙——智能选路讲解
  2. Java面试大厂名企高频真题--01基础篇
  3. No SLF4J providers were found.
  4. Java、JSP拍卖系统
  5. 用Springer LaTeX模板时使用BibTeX遇到的问题和解决过程
  6. python 爬虫学习:抓取智联招聘网站职位信息(一)
  7. 从程序员之死看 IT 人士如何摆脱低情商诅咒
  8. python locust post 参数拼接md5_HttpRunner接口自动化测试框架
  9. json字符串换java对象时遇到NoSuchMethodException的问题
  10. 支持DITA的CCMS、编辑器和工具