简体   繁体   English

如何使用ActiveRecord Rails 5打开和关闭新的数据库连接

[英]How do you open and close a new database connection with ActiveRecord Rails 5

I have some workloads that need to increase WORK_MEM and TEMP_BUFFERS . 我有一些工作量需要增加WORK_MEMTEMP_BUFFERS The problem is, once I do this, the connection (session) is not closed and is returned to the pool. 问题是,一旦执行此操作,连接(会话)就不会关闭并返回到池中。 This means the extra memory usage isn't temporary because I can't change TEMP_BUFFERS back without destroying the session. 这意味着额外的内存使用不是临时的,因为我不能在不破坏会话的情况下将TEMP_BUFFERS改回TEMP_BUFFERS

I imagine the workflow going something like this: 我想象工作流程是这样的:

conn = ActiveRecord.create_new_connection
conn.execute <<-SQL
  SET TEMP_BUFFERS TO '512MB';
  -- do some memory intensive 
  CREATE TEMPORARY TABLE .....;
  UPDATE ....;
SQL
conn.close

Or maybe there is a way to close the current connection and have the pool crate a new one... 或者也许有一种方法可以关闭当前连接,并让池创建一个新的池...

Seems like new_connection is a private method , which is what we need to manually create a connection outside being part of the pool, 好像new_connection是私有方法 ,这是我们需要手动创建的连接的一部分,而该连接不是池的一部分,

Then, upon inspecting the source code, you can do something like this: 然后,在检查源代码后,您可以执行以下操作:

connection_specification = ActiveRecord::Base.connection_pool.spec
# i.e.
#   connection_specification.adapter_method == 'postgresql_connection'
#   connection_specification.config == {:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :database=>"someapp_development"}

connection = ActiveRecord::Base.send(
  connection_specification.adapter_method, 
  connection_specification.config
)

# DEBUG:
puts connection.active?
# => true

connection.execute <<-SQL
  SET TEMP_BUFFERS TO '512MB';
  -- do some memory intensive 
  CREATE TEMPORARY TABLE .....;
  UPDATE ....;
SQL

connection.disconnect!

# DEBUG:
puts connection.active?
# => false

暂无
暂无

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

相关问题 你如何在Rails 3中调整ActiveRecord关联的范围? - How do you scope ActiveRecord associations in Rails 3? 如何配置ActiveRecord与数据库的默认打开连接以及如何对其进行验证 - How to configure ActiveRecord default open connection with database and how to verify it 如何使用Ruby on Rails创建新的ActiveRecord连接池? - How to create a new ActiveRecord Connection Pool with Ruby on Rails? 如何在Rails中向ActiveRecord模型添加自定义唯一性验证? - How do you add a custom uniqueness validation to an ActiveRecord model in Rails? 如何在非 Rails 应用程序中使用 ActiveRecord 创建一个新的 MySQL 数据库? - How to create a new MySQL database with ActiveRecord in a non-rails app? ActiveRecord Rails 如何在多态关联中查询关联模型 - ActiveRecord Rails How do you query for an associated model in a polymorphic association 无法打开rails console:生成数据库未配置,establish_connection引发ActiveRecord :: AdapterNotSpecified - Can't open rails console: production database not configured, establish_connection raises ActiveRecord::AdapterNotSpecified 如何防止Rails ActiveRecord中的数据库更改在before_create过滤器返回false时被回滚? - How do you prevent database changes inside a Rails ActiveRecord before_create filter from getting rolled back when it returns false? 关闭Postgre DB连接,并在轨道上使用其他DB rub​​y​​打开新连接 - Close the Postgre DB connection and open a new connection with different DB ruby on rails 如何在数据库中为块创建新行,或者在Rails中为每个模型创建新模型? - How do you create a new row for a block in a database or a new model for each in Rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM