使用 Mysql Ruby 设置连接超时
我正在使用 mysql (5.1) ruby (1.8.7) 连接器从数据库中提取一些数据。有些查询的运行时间往往比预期的要长,因此我想为这些查询设置连接超时。 相关帖子提出的解决方案是以下:
require 'rubygems'
require 'mysql'
# connect to the database
db = Mysql.real_connect( 'server', 'user', 'password', 'schema' )
# configure read timeout
db.options(Mysql::OPT_READ_TIMEOUT, 10)
# query and process
begin
db.query("select * from ..").each_hash do |row|
# process data if query returned
end
rescue Mysql::Error => err
# handle timeout
end
但是,这似乎对我不起作用。有什么想法为什么以及如何实现我所追求的目标吗?谢谢。
I'm working with mysql (5.1) ruby (1.8.7) connector to extract some data from a database. Some queries tend to run longer than desired, so I'd like to set a connection timeout on these. A solution proposed by a related post is the following:
require 'rubygems'
require 'mysql'
# connect to the database
db = Mysql.real_connect( 'server', 'user', 'password', 'schema' )
# configure read timeout
db.options(Mysql::OPT_READ_TIMEOUT, 10)
# query and process
begin
db.query("select * from ..").each_hash do |row|
# process data if query returned
end
rescue Mysql::Error => err
# handle timeout
end
However, this doesn't seem to work for me. Any ideas why and how to accomplish what I'm after? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能必须在实际连接之前设置选项。
You might have to set the options before actually connecting.