Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6375541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:37:43+00:00 2026-05-25T01:37:43+00:00

This code was taken from ActiveRecord 2.3.14’s gem class ConnectionHandler def establish_connection(name, spec) @connection_pools[name]

  • 0

This code was taken from ActiveRecord 2.3.14’s gem class ConnectionHandler

def establish_connection(name, spec)
  @connection_pools[name] = ConnectionAdapters::ConnectionPool.new(spec)
end

It seems each time ruby calls establish_connection on the model, it’s creating a new connection pool.

My question:

If I have 5 models that use establish_connection to the same database, is Rails smart enough to pick an already existing pool rather creating a new one with the same connection credentials? Does this also happen if my 5 models are subclasses of some abstract class that uses establish_connection? Will it always pick a connection from the @connection_pools if it exists?

Update 1

I’m talking about a concrete example. You have 5 models with 5 different connections, each time Rails uses a model it executes establish_connection. Looking at the code in ActiveRecord, when it executes establish_connection it creates a new pool with connections to that specific connection. What I’m wondering is whether each time Rails calls a model’s establish_connection, does it create a new pool or take the existing one.

Example: you come to my site and see a product list. You’ve just hit an action that calls Product.all, which executes establish_connection to some database on Amazon. Then, I come to the product list, what happens? Do I grab the established connection or am I creating a new pool with that connection?

Update 2

My guess is that first time Rails loads my models it’s creating pools with different connections. After, when I use some Model.method, it just grabs the connection associated with the model and executes the method.

I’m not sure what happens when 2 models have two equal connections (not in the abstract class but in self class). Will this produce two same connection pools, or is ActiveRecord smart enough to catch this case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:37:43+00:00Added an answer on May 25, 2026 at 1:37 am

    You really do not have to call establish_connection on each model. You can simply do next:

    ActiveRecord::Base.establish_connection(
     { :adapter => 'mysql2',
       :database => 'some_database',
       :host => 'localhost',
       :username => 'root',
       :password => "" }
    )
    

    and you will have access to connection. (This chunk of code has been extracted from real code(except database name 🙂 )).
    But according to API I think that Rails does not take existing connection from other model (correct me if I am wrong).
    Also here is a link to documentation. You can read more about the connection there.
    I hope I helped you alittle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code is taken from a SCJP practice test: 3. public class Bridge {
I use this code which is taken from MVC futures and attach the Attribute
I don't understand the rationale of this code, taken from javax.swing.event.EventListenerList docs: protected void
Why does this code (taken from the demo sample at jquery ui), doesn't work
code sample taken from MSDN public class Test { public static void Main() {
This is a small snippet of code taken from some of the examples that
I use comboboxes code taken from jquery examples . This is modified to use
This code is taken from a previous question, but my question directly relates to
I'm trying to run this code taken from Sun Java site (I didn't copy
Looking at this code below (taken from the git page ) var redis =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.