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

KPheasey

I have some workloads that need to increase WORK_MEM and TEMP_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.

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...

Jay-Ar Polidario

Seems like new_connection is a private method, which is what we need to manually create a connection outside being part of the pool,

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Redis in python, how do you close the connection?

How do you use a Connection Pool with ActiveJDBC instead of just Base.open & close everytime?

How do you open and close a gui in Roblox?

How do we close the database `connection` in sequelize

How to close database connection?

Close the Postgre DB connection and open a new connection with different DB ruby on rails

How do you open vimwiki in a new split?

How do you close netcat connection after receipt of data?

How do you close a websocket connection if the API page is invalid

How do you add a custom uniqueness validation to an ActiveRecord model in Rails?

How do you close open files using Swift?

How do you convert 1 minute open-high-low-close data to another timeframe(fx: 5 minute, 1 hour) in Python?

How to close a phalcon database connection?

How do I close, then re-open an Xodus database?

Copy an object to a new database in Rails ActiveRecord

Close and Open new Window PYQT5

How do you find open transactions on Windows Azure SQL Database?

How do you open extracted images from the MNIST database

How do you open a new JPanel window on a click of a button?

How do you open a custom URL in a new tab with content?

OleDbConnection: How to open and close a connection using a function

How do I open a session using JSch, close that session, and then open a new one using the same credentials?

Does ActiveRecord make a new database connection per model

Rails cant connect to database with ActiveRecord::establish_connection

How do you automatically add foreign keys to an existing Rails database?

How do I open a Rails Mysql database in Mysql Workbench?

How to close the database connection at the end of the program?

How to properly close the database connection in a lambda function?

How can I close the database connection in this approach?