I am using the new ReplicaSetConnection method for making a connection to my mongodb cluster. The change really comes down to replacing pymongo.Connection with pymongo.ReplicaSetConnection. I use the connection for my purposes and then I call end_request on the connection to make sure I flush the connection before I call disconnect() on the connection. This ensures that I dont have a large collection of half-connected sockets after a long run. This works great when I use Connection, but when I use ReplicaSetConnection pymongo complains that I’m trying to run end_request() on a database object despite the fact that i am most definitely calling this against the ReplicaSetConnection object. Is this something new in pymongo or is this an error in the driver? Below is a manual run through of the problem I’m experiencing.
>>> import pymongo
>>> s = pymongo.ReplicaSetConnection("192.168.1.1:27017, 192.168.1.2:27017", replicaSet='rep1', safe=True)
>>> s
ReplicaSetConnection([u'192.168.1.1:27017', u'192.168.1.2:27017'])
>>> s.read_preference = pymongo.ReadPreference.SECONDARY
>>> s
ReplicaSetConnection([u'192.168.1.1:27017', u'192.168.1.2:27017'])
>>> type(s)
<class 'pymongo.replica_set_connection.ReplicaSetConnection'>
>>> d = s['test']
>>> s.end_request()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.linux-x86_64/egg/pymongo/database.py", line 696, in __call__
TypeError: 'Database' object is not callable. If you meant to call the 'end_request' method on a 'Connection' object it is failing because no such method exists.
>>> s.disconnect()
>>> s
ReplicaSetConnection([u'192.168.1.1:27017', u'192.168.1.2:27017'])
ReplicaSetConnection in PyMongo 2.1 doesn’t support end_request(); it will in version 2.2 to be released in the next couple weeks. Meanwhile, there’s no need to call end_request() before disconnect. Disconnect will close all sockets.