How do I set the ActiveRecord query timeout for my

2019-01-26 13:42发布

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.

2条回答
地球回转人心会变
2楼-- · 2019-01-26 14:36

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.

查看更多
倾城 Initia
3楼-- · 2019-01-26 14:39

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.

查看更多
登录 后发表回答