Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the ActiveRecord query timeout for mysql?

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.

like image 698
esilver Avatar asked Mar 30 '11 01:03

esilver


People also ask

How do I set Activerecord timeout?

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.

How do I set query timeout in MySQL?

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.

What is MySQL default timeout?

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.


1 Answers

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.

like image 106
esilver Avatar answered Sep 28 '22 10:09

esilver