How do I set the ActiveRecord query timeout for mysql?

后端 未结 2 2070
刺人心
刺人心 2020-12-19 11:17

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条回答
  • 2020-12-19 11:44

    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.

    0 讨论(0)
  • 2020-12-19 11:47

    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.

    0 讨论(0)
提交回复
热议问题