Ensure you're always using the same connection
In Rails, if you want to ensure that you are using the same database connection from the connection pool to execute a number of commands, you can use the ActiveRecord::Base.with_connection
method.
This method yields a single connection from the pool for you to execute your commands against:
ActiveRecord::Base.with_connection do |conn|
conn.execute("select * from sessions")
end
It is important to note that the connection yielded is taken out of the pool until the processing in the block is complete.
Tweet