Hello!
2013/6/24 level077:
> 多个sql语句一次性传入会报错无,如下的sql:
> sql = 'select * from a;select * from b'
>
> 会报:failed to set keepalive: cannot be reused in the current connection
> state: 2
>
> 虽然不影响结果,还有人这样的吗
>
这个错误是因为你没有反复调用 read_result 方法直到读完所有的结果集,毕竟你的 SQL
中有多条语句,从而会产生多个结果集。如果一个 TCP
连接的接收缓冲区里还有未读的数据,则这样的连接是不能放入连接池的(如果能的话,下一次使用时就会读出老数据从而产生问题)。
引用一下 lua-resty-mysql 官方文档中关于 read_result 方法的相关段落:
“If more results are following the current result, a second err return
value will be given the string again. One should always check this
(second) return value and if it is again, then she should call this
method again to retrieve more results. This usually happens when the
original query contains multiple statements (separated by semicolon in
the same query string) or calling a MySQL procedure.”
如果你使用的是 query 方法,则需要注意它只会帮你调用第一次 read_result 方法,因此你总是需要检查 err 返回值是否为
'again' 从而据此决定是否需要再调用一次 read_result 方法。在这里再引用一下 query 方法官方文档:
“This (query method) is a shortcut for combining the send_query call
and the first read_result call.”
见 https://github.com/agentzh/lua-resty-mysql#query
Best regards,
-agentzh