mysql_pool_Lua Mysql连接池

--[[

使用方法:

local mysql = require "mysql_pool"

local ret, res, sqlstate = mysql:query(sql, true);

local tmp

if ret then

tmp = cjson.encode(res)

else

ngx.say("error")

end

--]]

--[[

注意事项:

1. 启用连接池需要开启 lua_code_cache on

2. 测试连接池时,线程数需要小于set_keepalive中的第二个参数(即连接数)

3. 根据程序里的SQL情况,设置set_timeout为适当的值(即连接超时??)

--]]

module("mysql_pool", package.seeall)

local mysql = require("resty.mysql")

local cfg = require "youngyedu.common.resources.config"

local mysql_pool = {}

--[[

先从连接池取连接,如果没有再建立连接.

返回:

false,出错信息.

true,数据库连接

--]]

function mysql_pool:get_connect()

if ngx.ctx[mysql_pool] then

return true, ngx.ctx[mysql_pool]

end

local client, errmsg = mysql:new()

if not client then

return false, "mysql.socket_failed: " .. (errmsg or "nil")

end

client:set_timeout(10000) --30秒

local options = {

host = cfg.db.prod.HOST,

port = cfg.db.prod.PORT,

user = cfg.db.prod.USER,

password = cfg.db.prod.PASSWORD,

database = cfg.db.prod.DATABASE

}

local result, errmsg, errno, sqlstate = client:connect(options)

if not result then

return false, "mysql.cant_connect: " .. (errmsg or "nil") .. ", errno:" .. (errno or "nil") ..

", sql_state:" .. (sqlstate or "nil")

end

local query = "SET NAMES " .. "utf8"

local result, errmsg, errno, sqlstate = client:query(query)

if not result then

return false, "mysql.query_failed: " .. (errmsg or "nil") .. ", errno:" .. (errno or "nil") ..

", sql_state:" .. (sqlstate or "nil")

end

ngx.ctx[mysql_pool] = client

-- 测试,验证连接池重复使用情况

--[[ comments by leon1509

local count, err = client:get_reused_times()

ngx.say("xxx reused times" .. count);

--]]

return true, ngx.ctx[mysql_pool]

end

--[[

把连接返回到连接池

用set_keepalive代替close() 将开启连接池特性,可以为每个nginx工作进程,指定连接最大空闲时间,和连接池最大连接数

--]]

function mysql_pool:close()

if ngx.ctx[mysql_pool] then

-- 连接池机制,不调用 close 而是 keeplive 下次会直接继续使用

-- lua_code_cache 为 on 时才有效

-- 60000 : pool_max_idle_time , 100:connections

ngx.ctx[mysql_pool]:set_keepalive(60000, 80)

-- 调用了 set_keepalive,不能直接再次调用 query,会报错

ngx.ctx[mysql_pool] = nil

end

end

--[[

查询

有结果数据集时返回结果数据集

无数据数据集时返回查询影响

返回:

false,出错信息,sqlstate结构.

true,结果集,sqlstate结构.

--]]

function mysql_pool:query(sql, flag)

local ret, client = self:get_connect(flag)

if not ret then

return false, client, nil

end

local result, errmsg, errno, sqlstate = client:query(sql)

while errmsg == "again" do

result, errmsg, errno, sqlstate = client:read_result()

end

self:close()

if not result then

errmsg = "mysql.query_failed:" .. (errno or "nil") .. (errmsg or "nil")

return false, errmsg, sqlstate

end

return true, result, sqlstate

end

return mysql_pool

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值