how mongodb autoreconnect?

118 views
Skip to first unread message

luocheng

unread,
Aug 13, 2012, 8:03:19 AM8/13/12
to mongod...@googlegroups.com
Hi all
    i want to autoreconnect to mongodb when some fails happen to mongodb replicatesets,the code of connection is as bellow:
           self.db_connection = ReplicaSetConnection("localhost:7017", replicaSet='nova_data')
           self.db_database = self.db_connection[self.db_name]

there are 10 processes writing some info to mongodb every 30 seconds,
when i shutdown the primary , relicate set  elect the new primary , but the write process write to mongodb successfully some times,
and then fail some times,and successfully some times again.
in fact ,it has already connect to the new primary ,but it will raise autoreconnection errors some times.
what's the reason ?

should i reassgin the self.db_database after the autoreconnection happens? how can i avoid the failure when autoreconnection happens?

PS: i use pymongo 2.1,mongodb 2.0.1






Jeremy Mikola

unread,
Aug 13, 2012, 11:55:55 AM8/13/12
to mongod...@googlegroups.com
In PyMongo's Replica Set: Handling Failover documentation, it mentions that the driver will attempt to reconnect after throwing an AutoReconnect exception. In your application, you then have the option of retrying the query or moving on. Election times can typical very between 10 seconds to a minute, so you should anticipate some delay when retrying the query.

You might also find some advice in this thread from last month.

Reply all
Reply to author
Forward
0 new messages