加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – Mysql2:关闭客户端连接

发布时间:2020-12-17 03:05:43 所属栏目:百科 来源:网络整理
导读:我正在使用Mysql2来查询 ruby上的数据库.我通过以下方式初始化连接: client = Mysql2::Client.new(:host = "localhost",:database = 'mydb',:username = "root") 查询成功完成后,如何关闭客户端连接?如果我不关闭它,我很快就会达到最大可能的打开连接数.
我正在使用Mysql2来查询 ruby上的数据库.我通过以下方式初始化连接:

client = Mysql2::Client.new(:host => "localhost",:database => 'mydb',:username => "root")

查询成功完成后,如何关闭客户端连接?如果我不关闭它,我很快就会达到最大可能的打开连接数.

感谢@joonty:

client.close

解决方法

使用client.close. From the docs:

Mysql2::Client#close

Immediately disconnect from the server,normally the garbage collector will disconnect automatically when a connection is no longer needed. Explicitly closing this will free up server resources sooner than waiting for the garbage collector.

您是否有多个长时间运行的进程只能在短时间内使用mysql连接?这应该是这是一个问题的唯一情况.如果您的进程正在结束,那么连接将作为垃圾收集的一部分关闭,因此您的问题出在其他地方.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读