# Multiple DB connections not working after first call

**URL:** <https://www.ruby-forum.com/t/multiple-db-connections-not-working-after-first-call/49176>\
**Category:** Rails\
**Created:** [November 30, 2005, 10:55pm UTC](https://www.ruby-forum.com/t/multiple-db-connections-not-working-after-first-call/49176 "2005-11-30T22:55:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@rails](https://www.ruby-forum.com/u/rails)\
**Post date:** [November 30, 2005, 10:55pm UTC](https://www.ruby-forum.com/t/multiple-db-connections-not-working-after-first-call/49176/1 "2005-11-30T22:55:44Z")

</div>

Hello!

I’ve looked through the mailing list archives, but havn’t found a viable  
answer for this question. I would like to have my rails app handle a  
couple tables in a legacy database. Looking at the wiki, comments in  
the  
rails code, and searching for past comments on this list, I’ve tried two  
different methods that are both broken. At least for PostgreSQL, I  
havn’t  
testing this bug with any other databases.

First method is establishing the connection in environment.rb like such:

MyModel.establish\_connection :my\_db

This only works the first time if the page loaded uses MyModel. All  
subsequent calls defaults to the connection for ActiveRecord::Base. I  
can  
see the entry in active\_connections, but it doesn’t seem to be used.

Second option is putting the extablish\_connection directly in the  
my\_model.rb file.

class MyModel \< ActiveRecord::Base  
establish\_connection :my\_db  
end

This works… until you start having your db complain about too many  
connections. It will open a new database connection each time you  
access  
the model, and it leaves all previous connections open. Taking a look  
at  
active\_connections will show multiple entries for MyModel.

Does anyone have a solution that will work, and won’t make multiple  
connections that will never be used again?

Thanks for any help,  
Ken…

---

<div class="post-metadata">

**Author:** ![rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@rails](https://www.ruby-forum.com/u/rails)\
**Post date:** [November 30, 2005, 11:03pm UTC](https://www.ruby-forum.com/t/multiple-db-connections-not-working-after-first-call/49176/2 "2005-11-30T23:03:51Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Nov 30, 2005, at 1:51 PM, Ken B. wrote:

> MyModel.establish\_connection :my\_db
> 
> This only works the first time if the page loaded uses MyModel.  
> All subsequent calls defaults to the connection for  
> ActiveRecord::Base. I can see the entry in active\_connections, but  
> it doesn’t seem to be used.

This was fixed in [http://dev.rubyonrails.org/ticket/2996](http://dev.rubyonrails.org/ticket/2996)

jeremy  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDjiEEAQHALep9HFYRAjEAAKC6NAp2UfRXMsdTj3u/Af3hEUvc8QCfXEls  
YUeLL3a4HPezp0ayAPBSHh4=  
=Ti8R  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@rails](https://www.ruby-forum.com/u/rails)\
**Post date:** [November 30, 2005, 11:40pm UTC](https://www.ruby-forum.com/t/multiple-db-connections-not-working-after-first-call/49176/3 "2005-11-30T23:40:14Z")

</div>

On Wed, 30 Nov 2005, Jeremy K. wrote:

> This was fixed in [http://dev.rubyonrails.org/ticket/2996](http://dev.rubyonrails.org/ticket/2996)
> 
> jeremy

Thank you! I’ve filed a bug report with Debian in the hopes that the  
fix  
will make it into the official Debian packages soon.
