# DBI Connect failure - can we retry please?

**URL:** <https://www.ruby-forum.com/t/dbi-connect-failure-can-we-retry-please/168826>\
**Category:** Ruby\
**Created:** [May 26, 2009, 2:29pm UTC](https://www.ruby-forum.com/t/dbi-connect-failure-can-we-retry-please/168826 "2009-05-26T14:29:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [May 26, 2009, 2:29pm UTC](https://www.ruby-forum.com/t/dbi-connect-failure-can-we-retry-please/168826/1 "2009-05-26T14:29:33Z")

</div>

Hi Guys,

I got a DBI connection that tries to connect to a source that sometimes  
has an unreliable connection behind it.

DBH = DBI.connect(‘dbi:ODBC:conenction\_name’, ‘username’, ‘password’)

It has failed a couple of times to connect and just bombs out the  
script. As this is to be an automated script it would be nice to be able  
to make a couple of attempts at trying to connect before bombing out.

Is anyone able to offer any code and advice as to how best to go about  
this? I have looked online but the help is limited.

Many thanks in advance,

Roger

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [May 26, 2009, 3:16pm UTC](https://www.ruby-forum.com/t/dbi-connect-failure-can-we-retry-please/168826/2 "2009-05-26T15:16:15Z")

</div>

On May 26, 2009, at 8:29 , Dave S. wrote:

> It has failed a couple of times to connect and just bombs out the  
> script. As this is to be an automated script it would be nice to be  
> able  
> to make a couple of attempts at trying to connect before bombing out.
> 
> Is anyone able to offer any code and advice as to how best to go about  
> this? I have looked online but the help is limited.

begin/rescue with a counter?

Michael G.  
grzm seespotcode net

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [May 26, 2009, 5:32pm UTC](https://www.ruby-forum.com/t/dbi-connect-failure-can-we-retry-please/168826/3 "2009-05-26T17:32:02Z")

</div>

> DBH = DBI.connect(‘dbi:ODBC:conenction\_name’, ‘username’, ‘password’)
> 
> It has failed a couple of times to connect and just bombs out the  
> script. As this is to be an automated script it would be nice to be  
> able  
> to make a couple of attempts at trying to connect before bombing out.
> 
> Is anyone able to offer any code and advice as to how best to go about  
> this? I have looked online but the help is limited.

gem install attempt

attempt{ dbh = DBI.connect(…) }

See [http://rubyforge.org/docman/view.php/735/485/README.html](http://rubyforge.org/docman/view.php/735/485/README.html) for more  
details.

Regards,

Dan
