The same logic still applies. Consider the following snippet:
poprocks@ubuntu:~$ cat t.lua
local cjson = require "cjson"
local mysql = require "resty.mysql"
local db = mysql:new()
local ok, err, errcode, sqlstate = db:connect({
host = "127.0.0.1",
port = 3306,
database = "foo",
user = "test_user",
password = "password"})
if not ok then
ngx.log(ngx.ERR, "failed to connect: ", err, ": ", errcode, " ", sqlstate)
return ngx.exit(500)
end
res, err, errcode, sqlstate = db:query("insert into bar values (1); insert into bar values (2); insert into bar values(3); select * from bar;")
if not res then
ngx.log(ngx.ERR, "bad result #1: ", err, ": ", errcode, ": ", sqlstate, ".")
return ngx.exit(500)
end
ngx.say("result #1: ", cjson.encode(res))
local i = 2
while err == "again" do
res, err, errcode, sqlstate = db:read_result()
if not res then
ngx.log(ngx.ERR, "bad result #", i, ": ", err, ": ", errcode, ": ", sqlstate, ".")
return ngx.exit(500)
end
ngx.say("result #", i, ": ", cjson.encode(res))
i = i + 1
end
local ok, err = db:set_keepalive(10000, 50)
if not ok then
ngx.log(ngx.ERR, "failed to set keepalive: ", err)
ngx.exit(500)
end
poprocks@ubuntu:~$ ./openresty/bin/resty ./t.lua
result #1: {"insert_id":0,"affected_rows":1,"server_status":10,"warning_count":0}
result #2: {"insert_id":0,"affected_rows":1,"server_status":10,"warning_count":0}
result #3: {"insert_id":0,"affected_rows":1,"server_status":10,"warning_count":0}
result #4: [{"t":1},{"t":1},{"t":1},{"t":2},{"t":3},{"t":1},{"t":2},{"t":3}]