On Nov 30, 1:49 am, Alexey Ilyichev <[email protected]> wrote:
> Hi.
>
> We use Sequel with jruby and postgresql. The thing is when we restart
> the db server, sequel raises an exception saying that connection has
> died. What I would like to do is to reconnect to DB and repeat the
> query in this situation. And sure I wouldn't like to repeat this logic
> all over the project. Any ideas how to do it?
>
> Thanks in advance!

This is the expected behavior.  Sequel raises a
DatabaseDisconnectError, which triggers the connection to be removed
from the pool, and a new connection is automatically made on the next
query.

If you want to automatically reconnect, wrap your code in a begin/
rescue block and retry if you get a DatabaseDisconnectError.  Note
that this can lead to problems, and doing so safely is application
dependent.  There's a reason it isn't done by default.

The safest way to do what you want would be to override the
Database#transaction method to call super inside a begin/rescue block,
and retry if a DatabaseDisconnectError is raised.  Then, as long as
you are using transactions for everything, it should generally work.
Outside of transactions, it's probably not safe to automatically
retry.  Note that you still need to be sure that all non-database
actions inside your transaction block are idempotent if you do this.
Also, there are probably race conditions depending when the database
connection is dropped, so even retrying just for transactions can
probably result in a transaction being applied twice if you are
unlucky.

A better solution would be to not restart your db server as often, or
to restart your application at the same time.

Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to