Mysql2:关闭客户端连接 [英] Mysql2: close client connection

查看:231
本文介绍了Mysql2:关闭客户端连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Mysql2 查询 ruby​​ 上的数据库。我通过以下方式初始化连接:

I am using Mysql2 to query a database on ruby. I initialize the connection by:

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

成功后,如何关闭客户端连接?如果我不关闭它,我很快就达到了可能打开的连接的最大数量。

Once the query is done successfully, how can I close the client connection? If I do not close it, I soon reach the maximum number of possible open connections.

to @joonty:

Thanks to @joonty:

client.close

推荐答案

使用 client.close 从文档


Mysql2 :: Client#close

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连接很短时间?这应该是这是一个问题的唯一情况。如果您的进程正在结束,那么连接将作为垃圾回收的一部分关闭,因此您的问题位于其他位置。

Have you got multiple long-running processes that only use the mysql connection for a short time? That should be the only case where this is an issue. If your processes are ending then the connection will be closed as part of garbage collection, so your issue lies elsewhere.

这篇关于Mysql2:关闭客户端连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆