新手问题 mysql2 如何关闭连接

perrywky · 2013年08月09日 · 最后由 perrywky 回复于 2013年08月09日 · 3471 次阅读

我写了一个脚本处理消息队列,执行一段时间后,会产生大量 mysql 连接,请问如何关闭 client.query 产生的连接?

这个......


client = Mysql2::Client.new (:host => "localhost", :username => "root")
client.query(".....")
client.close

#1 楼 @metal close 并不管用。。。

#2 楼 @perrywky

client.close
nil
client.query('....')
Mysql2::Error: closed MySQL connection
mysql> show processlist;
+----+------+-----------+--------------------+---------+------+-------+------------------+
| Id | User | Host      | db                 | Command | Time | State | Info             |
+----+------+-----------+--------------------+---------+------+-------+------------------+
|  9 | root | localhost | information_schema | Sleep   |   67 |       | NULL             |
| 10 | root | localhost | NULL               | Query   |    0 | init  | show processlist |
+----+------+-----------+--------------------+---------+------+-------+------------------+
2 rows in set (0.00 sec)

mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
| 10 | root | localhost | NULL | Query   |    0 | init  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)

#3 楼 @metal 这。。难道是我今天姿势不对。。

#4 楼 @perrywky 我觉得可能出在你代码上面,这里有个例子

https://gist.github.com/caspyin/2230396

#5 楼 @metal 问题找到了,的确是我程序的问题,我在另一个方法里新建了 client 但是没有 close,方法调用结束后这个 client 也没有自动释放连接 多谢你啦!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号