# Ping database to keep connection alive

**URL:** <https://www.ruby-forum.com/t/ping-database-to-keep-connection-alive/68505>\
**Category:** Rails\
**Created:** [September 13, 2006, 7:44am UTC](https://www.ruby-forum.com/t/ping-database-to-keep-connection-alive/68505 "2006-09-13T07:44:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![snacktime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@snacktime](https://www.ruby-forum.com/u/snacktime)\
**Post date:** [September 13, 2006, 7:44am UTC](https://www.ruby-forum.com/t/ping-database-to-keep-connection-alive/68505/1 "2006-09-13T07:44:27Z")

</div>

I’m running rails behind a firewall which times out idle tcp  
connections. What would be the best way to have rails ping the  
database every so many minutes? Or alternatively, is it possible to  
ping it myself before the session (which uses a database) is created?

Chris

---

<div class="post-metadata">

**Author:** ![snacktime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@snacktime](https://www.ruby-forum.com/u/snacktime)\
**Post date:** [September 13, 2006, 7:52am UTC](https://www.ruby-forum.com/t/ping-database-to-keep-connection-alive/68505/2 "2006-09-13T07:52:28Z")

</div>

On 9/12/06, snacktime [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m running rails behind a firewall which times out idle tcp  
> connections. What would be the best way to have rails ping the  
> database every so many minutes? Or alternatively, is it possible to  
> ping it myself before the session (which uses a database) is created?
> 
> Chris

Well it appears that I could use  
ActiveRecord::Base.connection.verify!, now the question is how to  
verify before the session is accessed.

---

<div class="post-metadata">

**Author:** ![snacktime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@snacktime](https://www.ruby-forum.com/u/snacktime)\
**Post date:** [September 13, 2006, 8:10am UTC](https://www.ruby-forum.com/t/ping-database-to-keep-connection-alive/68505/3 "2006-09-13T08:10:29Z")

</div>

On 9/12/06, snacktime [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m running rails behind a firewall which times out idle tcp  
> connections. What would be the best way to have rails ping the  
> database every so many minutes? Or alternatively, is it possible to  
> ping it myself before the session (which uses a database) is created?

The simplest way is to set up a cron job somewhere to curl [http://myapp](http://myapp).

You could also experiment with something like  
Thread.new do  
loop { ActiveRecord::Base.verify\_active\_connections!; sleep 300 }  
end

jeremy

---

<div class="post-metadata">

**Author:** ![snacktime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@snacktime](https://www.ruby-forum.com/u/snacktime)\
**Post date:** [September 13, 2006, 8:22am UTC](https://www.ruby-forum.com/t/ping-database-to-keep-connection-alive/68505/4 "2006-09-13T08:22:29Z")

</div>

On 9/12/06, Jeremy K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 9/12/06, snacktime [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I’m running rails behind a firewall which times out idle tcp  
> > connections. What would be the best way to have rails ping the  
> > database every so many minutes? Or alternatively, is it possible to  
> > ping it myself before the session (which uses a database) is created?
> 
> The simplest way is to set up a cron job somewhere to curl [http://myapp](http://myapp).

I think you are right, the simplest is probably the best.
