我正在编写一个使用mysql2
gem的简单Ruby脚本。
为了正确终止与数据库的连接并避免Too many connections
错误,我将连接存储到变量mysql
中,如下所示:
mysql = Mysql2::Client.new(:host => hst, :username => usr, :password => pass, :database => db, :connect_timeout => 30)
然后关闭连接:
mysql.close
当发生这种情况时,我得到:
closed MySQL connection
在控制台中。
如何在终端中没有#close
的情况下实施closed MySQL connection
找到的 def token do
client = OAuth2.Client.new([
strategy: OAuth2.Strategy.AuthCode, #default
client_id: "myClientID",
client_secret: "myClientSecret",
site: "https://www.googleapis.com",
redirect_uri: "https%3A%2F%2Fdevelopers.google.com%2Foauthplayground" #I HAVE NO IDEA WHAT SHOULD BE HERE???
])
token =
client
|> OAuth2.Client.put_param(:code, "myAuthCode")
|> OAuth2.Client.get_token!()
实例方法?
答案 0 :(得分:3)
你可以这样做:
def silence_stdout
$stdout = File.new( '/dev/null', 'w' )
yield
ensure
$stdout = STDOUT
end
并使用该方法执行close
silence_stdout{mysql.close}