简体   繁体   English

Ruby on Rails:如何在应用程序配置中设置数据库超时?

[英]Ruby on Rails: How to set a database timeout in application configuration?

I'd like to set my database to timeout requests that do not complete within a set amount of time, in order to prevent outlier requests from monopolizing the entire application. 我想将我的数据库设置为在一定时间内没有完成的超时请求,以防止异常请求独占整个应用程序。

Is there anything I can add to my Rails configuration files in order to set this? 有什么我可以添加到我的Rails配置文件中来设置它吗?

I tried to add a line I saw often online of timeout: 5000 to my database.yml, but that didn't seem to have any effect. 我试图添加一行我经常在线看到timeout: 5000到我的database.yml,但这似乎没有任何影响。

I tried to make calls to ActiveRecord::Base.connection.execute('set statement_timeout to 5000') in the environment.rb but that causes Rails to error out. 我试图在ActiveRecord::Base.connection.execute('set statement_timeout to 5000')调用ActiveRecord::Base.connection.execute('set statement_timeout to 5000') ,但这会导致Rails错误输出。

I'm running a Postgres database on Heroku, where I do not have direct database access, hence I cannot do this with database configuration directly. 我在Heroku上运行Postgres数据库,我没有直接的数据库访问权限,因此我不能直接使用数据库配置。 Even if I remotely execute that command from the Heroku console, they can restart my application at any time, and if this isn't re-executed as the application starts, my change gets lost. 即使我从Heroku控制台远程执行该命令,他们也可以随时重启我的应用程序,如果在应用程序启动时没有重新执行,我的更改就会丢失。

database.yml: database.yml的:

defaults: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  min_messages: warning
  variables:
    statement_timeout: 5000

得到了这个工作,只需要在最后一行包括environment.rb中的行,而不是在开头或块中。

试试这个语法:

SET statement_timeout = 5000;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM