如何设置ActiveRecord的查询超时为MySQL?如何设置、ActiveRecord、MySQL

2023-09-08 18:50:26 作者:5.孤身自傲

如何设置在ActiveRecord的MySQL查询超时?我想将其设置为东西很短,像10-15ms。这是一个西纳特拉红宝石Web应用程序。

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.

感谢。

推荐答案

那么,这样看来,每这些线路29和30 mysql_adapter.rb,

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]

需要一个简单的只有一个read_timeout和write_timeout值添加到.yaml数据库配置文件。

One need simply only add a read_timeout and write_timeout value to the .yaml database config file.

因此​​,

development:
  adapter: mysql
  encoding: utf8
  database: app_development
  pool: 5
  username: root
  password: 
  write_timeout: 1
  read_timeout: 1

应该做的伎俩,以每人设置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.