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.
According to docs you should set the "checkout_timeout" option in your database configuration file. checkout_timeout: number of seconds to block and wait for a connection before giving up and raising a timeout error (default 5 seconds). Save this answer.
Log in to your server by using Secure Shell® (SSH). Use the sudo command to edit my. cnf , the MySQL® configuration file. Locate the timeout configuration and make the adjustments that fit your server.
MySQL has its wait_timeout variable default value set to 28800 seconds (8 hours). Therefore, if both sides of the connection still keep the defaults, the problem will never happen, as MySQL will never timeout a connection before Stash does it.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With