# Using an external database behind a firewall

**URL:** <https://www.ruby-forum.com/t/using-an-external-database-behind-a-firewall/61642>\
**Category:** Rails\
**Created:** [May 25, 2006, 8:12pm UTC](https://www.ruby-forum.com/t/using-an-external-database-behind-a-firewall/61642 "2006-05-25T20:12:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![seanhussey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@seanhussey](https://www.ruby-forum.com/u/seanhussey)\
**Post date:** [May 25, 2006, 8:12pm UTC](https://www.ruby-forum.com/t/using-an-external-database-behind-a-firewall/61642/1 "2006-05-25T20:12:44Z")

</div>

Hi everyone,

There’s a separate database I’m using to get mail group info for our  
users (Sympa), and I’ve got Rails configured to pull data directly  
from it via an entry in database.yml

The issue we’re having is that the database server in question is  
behind an internal firewall that has idle timeouts set at 30 minutes.  
If the connection isn’t used (like, say, overnight), the firewall  
kills it. But Rails thinks it’s still open! Not so good.

Any ideas on how I can check for that and work around it?

This is also a point in my favor for caching the data locally and  
updating it every so often. That would be “live enough” for me, but  
not for others.

Thanks!

Sean

---

<div class="post-metadata">

**Author:** ![seanhussey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@seanhussey](https://www.ruby-forum.com/u/seanhussey)\
**Post date:** [May 25, 2006, 8:28pm UTC](https://www.ruby-forum.com/t/using-an-external-database-behind-a-firewall/61642/2 "2006-05-25T20:28:23Z")

</div>

> This is also a point in my favor for caching the data locally and  
> updating it every so often. That would be “live enough” for me, but  
> not for others.

> **[ActiveRecord::ConnectionAdapters::AbstractAdapter](https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/AbstractAdapter.html#M000753)**
>
> Active Record supports multiple database systems. AbstractAdapter and related classes form the abstraction layer which makes this possible.

maybe a combination of active() and reconnect! would do the trick?

Or failing that a cron job that runs every 20 minutes and performs a  
simple query?

-philip

---

<div class="post-metadata">

**Author:** ![seanhussey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@seanhussey](https://www.ruby-forum.com/u/seanhussey)\
**Post date:** [May 25, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/using-an-external-database-behind-a-firewall/61642/3 "2006-05-25T21:41:23Z")

</div>

So, something like this? (Assuming the AR model corresponding to the  
external DB is Mailinglist):

def show  
@person = Person.find(params[:id])

reconnect! if !Mailinglist.connection.active?  
@mailinglists = Mailinglist.find\_for\_user(@person.email)  
end

Looks exactly like what the doctor ordered! Thanks!

As far as the Rails-way, where would I handle whether or not this DB  
was even available? (Given the firewall’s history, that’s a real  
risk.)

Sean

---

<div class="post-metadata">

**Author:** ![seanhussey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@seanhussey](https://www.ruby-forum.com/u/seanhussey)\
**Post date:** [May 25, 2006, 9:47pm UTC](https://www.ruby-forum.com/t/using-an-external-database-behind-a-firewall/61642/4 "2006-05-25T21:47:43Z")

</div>

Er, that would be Mailinglist.connection.reconnect! of course.

---

<div class="post-metadata">

**Author:** ![seanhussey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@seanhussey](https://www.ruby-forum.com/u/seanhussey)\
**Post date:** [May 25, 2006, 10:16pm UTC](https://www.ruby-forum.com/t/using-an-external-database-behind-a-firewall/61642/5 "2006-05-25T22:16:41Z")

</div>

One day I’ll actually learn to try this out before replying.

Anyway, it looks like Rails thinks the connection is still active,  
even though the firewall has killed it. As such, active? returns  
true, and so we don’t reconnect.

Is it not advisable to connect/disconnect every time I want to get at  
that data? What a bear. Maybe if my exception handler catches and  
reconnects…but that could bring up other issues.

Besides, it doesn’t look like I’m getting an Exception per se.  
“Broken Pipe” is all.
