support for automatic reconnections to mongo?

371 views
Skip to first unread message

Robby D

unread,
Jan 22, 2012, 2:19:17 PM1/22/12
to MongoEngine Users
Hey guys, I've been using MongoEngine and am pretty happy with it. One
issue I did notice is that from time to time (especially if I make a
rule change on my firewall), the connection between mongoDB and my app
servers will be dropped. Most things recover well and reconnect, but
my app stop working because the mongo connection isn't reestablished.
I see that pymongo basically just leaves handling this AutoReconnect
error as an exercise for the reader, so to say.

A few questions:

1) Does mongoengine have any plans to support automatic reconnection?
2) If not, does anyone have a snippet of code that they use with
Django/mongoengine that actually works? I've found numerous ones on
the web and am having a big challenge getting them to work properly,
including https://gist.github.com/1393029 (which doesn't recover at
all...I get recusion errors) and http://pastebin.com/HE8LSp5v (which
will recover if mongo comes back up, but will not properly count the #
of attempts and error out...meaning it's just an infinite loop of
retries until mongo comes back up).

Any help here would be welcome!

Thanks,

Robby

Ross Lawley

unread,
Feb 2, 2012, 5:01:42 AM2/2/12
to mongoeng...@googlegroups.com
Hi Robby,

Yes generally its left as an exercise for the user, as it depends on your app and situation as what is best to do if a connection fails. 

I'm happy to look into this and try and make it easier, also happy for pull requests!  If you add a ticket to github I'll schedule it for post 0.6.

Ross

Robby D

unread,
Feb 2, 2012, 9:16:46 AM2/2/12
to MongoEngine Users
Hey Ross, thanks! Ticket made.

On Feb 2, 5:01 am, Ross Lawley <ross.law...@gmail.com> wrote:
> Hi Robby,
>
> Yes generally its left as an exercise for the user, as it depends on your
> app and situation as what is best to do if a connection fails.
>
> I'm happy to look into this and try and make it easier, also happy for pull
> requests!  If you add a ticket to github I'll schedule it for post 0.6.
>
> Ross
>
>
>
>
>
>
>
> On Sun, Jan 22, 2012 at 7:19 PM, Robby D <rob...@paramise.com> wrote:
> > Hey guys, I've been using MongoEngine and am pretty happy with it. One
> > issue I did notice is that from time to time (especially if I make a
> > rule change on my firewall), the connection between mongoDB and my app
> > servers will be dropped. Most things recover well and reconnect, but
> > my app stop working because the mongo connection isn't reestablished.
> > I see that pymongo basically just leaves handling this AutoReconnect
> > error as an exercise for the reader, so to say.
>
> > A few questions:
>
> > 1) Does mongoengine have any plans to support automatic reconnection?
> > 2) If not, does anyone have a snippet of code that they use with
> > Django/mongoengine that actually works? I've found numerous ones on
> > the web and am having a big challenge getting them to work properly,
> > includinghttps://gist.github.com/1393029(which doesn't recover at

inactivist

unread,
Aug 22, 2013, 9:04:23 AM8/22/13
to mongoeng...@googlegroups.com
I couldn't find anything relevant in the MongoEngine documentation or issues list.  

Are there documented best practices for this?  Or is it still a roll-your-own proposition?  

Ross Lawley

unread,
Aug 23, 2013, 5:54:01 AM8/23/13
to mongoeng...@googlegroups.com
Hi,

It really is a case by case scenario as to what is the correct semantic is for your application.

Ross 


--
You received this message because you are subscribed to the Google Groups "MongoEngine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoengine-us...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

inactivist

unread,
Aug 23, 2013, 7:46:14 AM8/23/13
to mongoeng...@googlegroups.com
Fair enough, but I don't see many examples of how to deal with it properly within the context of MongoEngine operations.  Just retry the operation, I suppose.

I did find this example of using a proxy to perform retries automatically with an adaptive backoff.

The hunt continues.  =)

-Mike

inactivist

unread,
Aug 23, 2013, 7:58:40 AM8/23/13
to mongoeng...@googlegroups.com
I found a relevant discussion in this group.  

I'd say my confusion stems from my lack of understanding exactly what the AutoReconnect exception means.

For example, here's an excerpt from the PyMongo docs:

Raised when a connection to the database is lost and an attempt to auto-reconnect will be made.

In order to auto-reconnect you must handle this exception, recognizing that the operation which caused it has not necessarily succeeded. Future operations will attempt to open a new connection to the database (and will continue to raise this exception until the first successful connection is made).

The operation which caused it has not necessarily succeeded... That's a bit vague.  So I need to verify that the operation indeed failed before retrying?  For example an insert into the collection might have succeeded even though AutoReconnect was raised?  (I realize this question is probably outside the scope of this group, which is MongoEngine and not PyMongo...)

The quest continues...

On Friday, August 23, 2013 2:54:01 AM UTC-7, Rozza wrote:

inactivist

unread,
Aug 23, 2013, 8:29:00 AM8/23/13
to mongoeng...@googlegroups.com
This article seems helpful as it discusses scenarios and coping strategies.


On Friday, August 23, 2013 2:54:01 AM UTC-7, Rozza wrote:

Oskari Petas

unread,
Feb 11, 2016, 4:01:13 AM2/11/16
to MongoEngine Users
Hi,

I' returning to this old thread: Is there any progress regarding this? Is there any best practice on how to handle the AutoReconnect exception?
Reply all
Reply to author
Forward
0 new messages