How can I set the mysql query timeout in ActiveRecord? I wish to set it to something very short, like 10-15ms. This is for a Sinatra ruby web app.
Thanks.
How can I set the mysql query timeout in ActiveRecord? I wish to set it to something very short, like 10-15ms. This is for a Sinatra ruby web app.
Thanks.
Well, it would appear that per these lines 29 and 30 in mysql_adapter.rb,
@connection.options(Mysql::OPT_READ_TIMEOUT, @config[:read_timeout]) if @config[:read_timeout]
@connection.options(Mysql::OPT_WRITE_TIMEOUT, @config[:write_timeout]) if @config[:write_timeout]
One need simply only add a read_timeout and write_timeout value to the .yaml database config file.
Thus,
development:
adapter: mysql
encoding: utf8
database: app_development
pool: 5
username: root
password:
write_timeout: 1
read_timeout: 1
Should do the trick to set read and write timeouts of 1 sec apiece. Unfortunately this does not allow you to set sub-second timeouts.
You could also set it in a per-connection basis like this:
ActiveRecord::Base.connection.instance_variable_get('@connection').instance_variable_set('@read_timeout', 0)
I'm doing this for instance to have a default read_timeout, but override it for my long-running nightly scripts.