Setting a connection timeout with Mysql Ruby
-
27-10-2019 - |
Question
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.
Solution
You might have to set the options before actually connecting.
db = Mysql.init
db.options(Mysql::OPT_READ_TIMEOUT, 10)
db.real_connect( 'server', 'user', 'password', 'schema' )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow