forked from cfadmin-cn/cfadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mysql.lua
More file actions
54 lines (51 loc) · 1.25 KB
/
test_mysql.lua
File metadata and controls
54 lines (51 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
-- 测试MySQL
local mysql = require "protocol.mysql"
local config = {
host = "localhost",
port = 3306,
database = "mysql",
user = "root",
password = "123456789"
}
-- 创建->查询->销毁100次mysql连接
local times = 100
for i = 1, times do
local db, err = mysql:new()
if not db then
return
end
local ok, err, errno, sqlstate = db:connect(config)
if not ok then
return print("连接失败.", i, err)
end
local resp, err = db:query("select * from user")
if not resp then
return print('查询失败', err)
end
db:close()
end
print("创建并销毁"..tostring(times).."次MySQL连接成功")
-- 保存mysql连接池 -> 销毁连接池
local pool = {}
for i = 1, times do
local db, err = mysql:new()
if not db then
return
end
local ok, err, errno, sqlstate = db:connect(config)
if not ok then
return print("连接失败.", i, err)
end
local resp, err = db:query("select * from user")
if not resp then
return print('查询失败', err)
end
pool[#pool+1] = db
end
print('创建'..tostring(times)..'MySQL连接成功')
for _, db in ipairs(pool) do
db:close()
end
pool = nil
print('销毁MySQL连接')
print("测试完成")