On Jan 6, 3:44 pm, "Aaron D. Gifford" <[email protected]> wrote:
> If the database is hosted on a different box (and sometimes even if
> it's on the same host), don't forget that there may be a stateful
> firewall that is expiring state and denying packets (thus causing the
> disconnect).
>
> I've had to adapt a bunch of my long-running applications to catch the
> MySQL disconnect and retry because of such things.
>
> For one Sinatra app, I added a middleware class to wrap the app and
> automagically restart it, something like:
>
> class DBRetryWrapper
> def initialize(app, maxtries=5)
> @app = app
> @maxtries = maxtries
> end
>
> def call(env)
> tries = 0
> begin
> @app.call(env)
> rescue Sequel::DatabaseDisconnectError, Mysql::Error => e
> if /server has gone away/.match(e.message)
> tries += 1
> raise e if tries > @maxtries
> retry
> end
> raise e
> end
> end
> end
Just note that you need to be careful doing that, as it has problems
unless your actions are idempotent. Example:
@app = Proc{|e| DB[:a].insert(:b=>params[:c]); DB[:d].first}
If the second query fails with a disconnect error, you'll end up
inserting into table a twice (or more).
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.