在 Rails 中为 SQL 查询设置超时值 [英] Set Timeout Value for SQL query in Rails

查看:38
本文介绍了在 Rails 中为 SQL 查询设置超时值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 ActiveRecord::Base.connection.execute(sql) 在 Rails 中执行一个很长的查询.

I want to execute a very long query in Rails using ActiveRecord::Base.connection.execute(sql).

但是,查询一直超时.是否可以更改此特定查询的超时值,而不必更改 database.yml 中所有查询的超时值?

However, the query keeps timing out. Is it possible to change the timeout value for this specific query instead of having to change the timeout value for all queries in database.yml?

谢谢

推荐答案

# in database.yml
production: &prod
  adapter: whatever
  timeout: 5000 

long_connection_production:
  <<: prod
  timeout: 10000

# app/models/long_connection.rb
class LongConnection < ActiveRecord::Base
  establish_connection "long_connection_#{Rails.env}"

  def self.do_thing_that_takes_a_long_time
    connection.execute(sql)
  end
end

这篇关于在 Rails 中为 SQL 查询设置超时值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆