Home  >  Article  >  Database  >  MySQL问题解决:Commands out of sync; you can't run this command n

MySQL问题解决:Commands out of sync; you can't run this command n

WBOY
WBOYOriginal
2016-06-07 17:27:543719browse

在某个场景下,我们在向table1中insert一条记录后,需要得到得到它的ID,然后update与之匹配的另一张表table2中的记录。由于inser

录制程序有一功能:将录制的文件信息写入MySQL数据库,供BS系统查询。
 
因此封装了一个MySQL类,进行数据库操作。
 
主要接口为Update():执行SQL语句。
 
现在问题来了:
(一)在某个场景下,我们在向table1中insert一条记录后,需要得到得到它的ID,然后update与之匹配的另一张表table2中的记录。由于insert本身并不返回结果集,因此我们无法直接得到插入记录的ID。
 
那该怎么办呢?
之前从BS组得到的方法是:在table1中执行insert后,立即执行另一条语句:“select @@IDENTITY;”;该语句会返回最后插入的那条记录的ID,这样问题就解决了。
 
但是,在一次codeview上,头儿提出了一个问题:如果在多线程中,在向table1执行insert后,另一个线程也执行了sql语句对数据库进行操作,这时再执行“select @@IDENTITY;”,能得到正确的结果吗?
很显然,确实存在这种问题:即我们无法保证整个过程的原子性,但这可以通过加锁来解决。
头儿又问:那这时候如果其他的客户端连接到数据库,执行了查询,会更改“select @@IDENTITY;”的结果吗?
当时没考虑到这一点,后来通过查询mysql手册(),发现系统变量IDENTITY的作用范围是“SESSION”,也就是其他客户端、其他连接执行的query不会更改本次连接的IDENTITY变量的值,因此不会有影响。
 
后来发现mysql提供了内部函数LAST_INSERT_ID(),我认为比“select @@IDENTITY;”更合理。
 
(二)另一个问题,,针对有些查询,我们需要获取并处理结果集。
原来是在Mysql类中封装了一个成员变量MYSQL_RES* m_res,用来保存有返回的查询结果集。这样一来,我们执行Update()的后,如果需要获取结果集就调用GetQueryResult()来获取结果集,用完之后再释放掉。
但是问题在于:在多线程环境中,我们可能有多个Update()需要获取结果集,但是如果它们共用一个成员变量m_res来存储结果集的话,我们必须等待一次查询使用完它的结果集并释放后才能执行下一次的Update(),否则MySQL就会报错:“”,因为此时m_res还未被释放,不能执行下一次查询。
 
针对以上问题,我们的解决办法是:对查询接口做调整。
一共提供两个查询接口:
(1)Update():执行查询;针对问题(一),开启multi-query支持,允许一次执行多条sql语句,具体操作参看();这样我们在Upadte时使用类似

?

insert into  t_alarm_record_file  (recordPath,recordName,hostIp,startTime,endTime,deviceId,programNumber,deviceType,interfaceNo,alarmType,alarmTime) values ('/figure/data/AlarmRecord/StreamTS/1-码流_魅力音 乐主路/2011-11-07/20111107150116.ts','','10.0.60.2','2011-11-07 15:01:16','1970-01-01 08:00:00',37486602,3905,'0',1,12,'2011-11-07 15:01:20');update  t_alarm  set fileId = LAST_INSERT_ID()  where deviceId = 37486602 and programNumber = 3905 and alarmType = 12 and alarmDate = '2011-11-07 15:01:20' and fileId is null


 的语句一次执行两条sql语句就可完美的解决这种问题了;
(2)Update2():执行查询,并返回结果集给调用者,由调用者自己处理并决定何时释放该结果集,由于该结果集是以参数的形式返回的,因此多线程不会受到影响。
 
但在实际测试中,还是出现问题:"Commands out of sync; you can't run this command now",我们的日志显示:?

2011-11-07 15:01:28,660: INFO  : Update OK!sql: insert into  t_alarm_record_file  (recordPath,recordName,hostIp,startTime,endTime,deviceId,programNumber,deviceType,interfaceNo,alarmType,alarmTime) values ('/figure/data/AlarmRecord/StreamTS/1-码流_魅力音 乐主路/2011-11-07/20111107150116.ts','','10.0.60.2','2011-11-07 15:01:16','1970-01-01 08:00:00',37486602,3905,'0',1,12,'2011-11-07 15:01:20');update  t_alarm  set fileId = LAST_INSERT_ID()  where deviceId = 37486602 and programNumber = 3905 and alarmType = 12 and alarmDate = '2011-11-07 15:01:20' and fileId is null

2011-11-07 15:01:35,331: ERROR  : Update failed!sql: insert into  t_alarm_record_file  (recordPath,recordName,hostIp,startTime,endTime,deviceId,programNumber,deviceType,interfaceNo,alarmType,alarmTime) values ('/figure/data/AlarmRecord/StreamTS/1-码流_魅力音 乐主路/2011-11-07/20111107150116.ts','','10.0.60.2','2011-11-07 15:01:16','1970-01-01 08:00:00',37486602,3905,'0',1,13,'2011-11-07 15:01:27');update  t_alarm  set fileId = LAST_INSERT_ID()  where deviceId = 37486602 and programNumber = 3905 and alarmType = 13 and alarmDate = '2011-11-07 15:01:27' and fileId is null, ERROR:Commands out of sync; you can't run this command now


在第一次调用Update()执行多条sql语句时成功,但以后的所有调用都失败了。
 
经过查找,发现问题在于:在Update()中执行多条sql语句时,

如果仅仅是插入等不需要返回值的SQL语句,也一样得读完整个resault集并释放,最小化的写法:
 
do
{
    result = mysql_store_result( mysql );
    mysql_free_result(result);
}while( !mysql_next_result( mysql ) );
 
经过这么一处理,问题终于解决了。

linux

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn