# Database connections...so many connections!

**URL:** <https://www.ruby-forum.com/t/database-connections-so-many-connections/74068>\
**Category:** Rails\
**Created:** [February 27, 2006, 10:45am UTC](https://www.ruby-forum.com/t/database-connections-so-many-connections/74068 "2006-02-27T10:45:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [February 27, 2006, 10:45am UTC](https://www.ruby-forum.com/t/database-connections-so-many-connections/74068/1 "2006-02-27T10:45:41Z")

</div>

Hello all.

I am currently running a very simple ruby app. It connects to an Oracle  
server using the OCI8 adapter.

I have two controllers, each one connects to a different schema on the  
server, I looked at the WIKI that had details of multiple database  
connections  
([Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowtoUseMultipleDatabases)), I  
moved the code from the application controller into each controller (as  
I thought it was prettier / easier / simpler ) . In each controller is  
the following code:

class XYZController \< ApplicationController  
before\_filter :change\_database

def change\_database  
ActiveRecord::Base.establish\_connection(  
:adapter =\> “oci”,  
:database =\> “”,  
:host =\> “**",  
:username =\> "**”, # Differs between controller  
:password =\> “\*\*\*\*\*\*\*\*” # Differs between controller  
)  
end

The problem is that with almost every action that performs a search a  
new connection is made to the oracle server despite the fact that one  
already exists for that schema (Looking at the oracle connection manager  
you see 2 connections for ruby from my machine, then it keeps rising).

Does anyone know how to limit the connections to two (One for each  
database, it is a low volume application). Is this a consequence of me  
diverting from the code used in the WIKI?

Many thanks

Jeff

---

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [February 27, 2006, 10:47am UTC](https://www.ruby-forum.com/t/database-connections-so-many-connections/74068/2 "2006-02-27T10:47:21Z")

</div>

> def change\_database  
> ActiveRecord::Base.establish\_connection(  
> :adapter =\> “oci”,  
> :database =\> “”,  
> :host =\> “\*\*\*\***",  
> :username =\> "", # Differs between controller  
> :password =\> "**” # Differs between controller  
> )  
> end
> 
> The problem is that with almost every action that performs a search a  
> new connection is made to the oracle server despite the fact that one  
> already exists for that schema (Looking at the oracle connection manager  
> you see 2 connections for ruby from my machine, then it keeps rising).
> 
> Does anyone know how to limit the connections to two (One for each  
> database, it is a low volume application). Is this a consequence of me  
> diverting from the code used in the WIKI?
> 
> Many thanks
> 
> Jeff

To clarify one point. I seem to remember reading somewhere that if you  
use ActiveRecord::Base.establish\_connection and a matching connection  
already exists in the hash of database connections then it would use  
that instead of creating a new one. Am I wrong?

Thanks

Jeff

---

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [February 27, 2006, 2:56pm UTC](https://www.ruby-forum.com/t/database-connections-so-many-connections/74068/3 "2006-02-27T14:56:20Z")

</div>

> > Jeff
> 
> To clarify one point. I seem to remember reading somewhere that if you  
> use ActiveRecord::Base.establish\_connection and a matching connection  
> already exists in the hash of database connections then it would use  
> that instead of creating a new one. Am I wrong?
> 
> Thanks
> 
> Jeff

Tried adding a remove\_connection in an after\_filter which to my mind  
should mean there are no connections active to the database unless  
WEBRICK is processing something but the inactive connections are still  
there.

I also tried using

def database\_connect  
unless ActiveRecord::Base.connected?  
ActiveRecord::Base.establish\_connection(  
:adapter =\> “oci”,  
:database =\> “”,  
:host =\> “oracle7”,  
:username =\> “vcgeneral”,  
:password =\> “manhattan”  
)  
end  
end

But that would always return true so I can’t use that as a control.

Any bright ideas to answer my relatively dim ones?

Thanks

Jeff

---

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [February 27, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/database-connections-so-many-connections/74068/4 "2006-02-27T15:40:36Z")

</div>

Ok, having done some more reading and shtuff.

It looks like I was trying to control the database connections in the  
wrong area. I was trying to control it in the controllers whereas I  
should have been using the models.

90% of my models access one database. For that I am using the standard  
connection as setup in the YAML file.

For the one model that is in the other database I add

unless connected?  
establish\_connection(  
:adapter =\> “oci”,  
:database =\> “”,  
:host =\> “_**",  
:username =\> "**__",  
:password =\> "_\*\*\*”  
)  
end

I could also add this to a super class of the other models and have them  
all inherit from that just to make sure the connections are clear.

Thanks to all who read through this in hope of helping

Jeff

---

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [February 27, 2006, 6:13pm UTC](https://www.ruby-forum.com/t/database-connections-so-many-connections/74068/5 "2006-02-27T18:13:20Z")

</div>

I’t seems that the really smart ones, are able to answer their own  
questions  
:). Thanks for the thread. One question though, did you put this  
code in  
a def in a model class or in the database.yml file?

-Larry

On 2/27/06, Jeff J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> For the one model that is in the other database I add
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Best Regards,  
-Larry  
“Work, work, work…there is no satisfactory alternative.”  
— E.Taft Benson

---

<div class="post-metadata">

**Author:** ![jjantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjantz](https://www.ruby-forum.com/u/jjantz)\
**Post date:** [February 28, 2006, 12:21am UTC](https://www.ruby-forum.com/t/database-connections-so-many-connections/74068/6 "2006-02-28T00:21:32Z")

</div>

Larry K. wrote:

> I’t seems that the really smart ones, are able to answer their own  
> questions  
> :). Thanks for the thread. One question though, did you put this  
> code in  
> a def in a model class or in the database.yml file?
> 
> -Larry
> 
> On 2/27/06, Jeff J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > For the one model that is in the other database I add
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> –  
> Best Regards,  
> -Larry  
> “Work, work, work…there is no satisfactory alternative.”  
> — E.Taft Benson

Smart? No, sheer bloody mindedness and borderline psychotic inability to  
let a computer get the best of me?..oh yes, I showed it!

This code was in the model.

Class news \< ActiveRecord::Base

unless connected?  
establish\_connection(  
:adapter =\> “oci”,  
:database =\> “”,  
:host =\> “_**",  
:username =\> "**__",  
:password =\> "_\*\*\*”  
)  
end

def index  
end

etc.

end

Any model that doesn’t specifically have an establish connection like  
that chunk code will use what is in the database.yml file which I have  
setup like a normal project ( translation, I did whatever was in the  
tutorials 😉 ).

Jeff
