diff --git a/pymongo/command_cursor.py b/pymongo/command_cursor.py index d99a5fe1a..513913c53 100644 --- a/pymongo/command_cursor.py +++ b/pymongo/command_cursor.py @@ -120,7 +120,7 @@ class CommandCursor(object): # Don't send kill cursors to another server after a "not master" # error. It's completely pointless. self.__killed = True - client.disconnect() + client._disconnect() raise self.__id = response["cursor_id"] if self.__id == 0: diff --git a/pymongo/cursor.py b/pymongo/cursor.py index fff51834d..64b3e1a15 100644 --- a/pymongo/cursor.py +++ b/pymongo/cursor.py @@ -1038,7 +1038,7 @@ class Cursor(object): self.__killed = True # Make sure exhaust socket is returned immediately, if necessary. self.__die() - client.disconnect() + client._disconnect() raise self.__id = response["cursor_id"] diff --git a/pymongo/database.py b/pymongo/database.py index 1f725b22c..09f2ff0d3 100644 --- a/pymongo/database.py +++ b/pymongo/database.py @@ -436,8 +436,8 @@ class Database(common.BaseObject): if check: msg = "command %s on namespace %s failed: %%s" % ( repr(command).replace("%", "%%"), self.name + '.$cmd') - helpers._check_command_response(result, self.connection.disconnect, - msg, allowable_errors) + helpers._check_command_response( + result, self.connection._disconnect, msg, allowable_errors) return result, cursor.conn_id @@ -756,7 +756,7 @@ class Database(common.BaseObject): if error_msg is None: return None if error_msg.startswith("not master"): - self.__connection.disconnect() + self.__connection._disconnect() return error def last_status(self): diff --git a/pymongo/master_slave_connection.py b/pymongo/master_slave_connection.py index 5fb862cd0..7ba70387c 100644 --- a/pymongo/master_slave_connection.py +++ b/pymongo/master_slave_connection.py @@ -197,6 +197,10 @@ class MasterSlaveConnection(BaseObject): .. seealso:: Module :mod:`~pymongo.mongo_client` .. versionadded:: 1.10.1 """ + self._disconnect() + + def _disconnect(self): + """Internal disconnect helper.""" self.__master.disconnect() for slave in self.__slaves: slave.disconnect() @@ -207,7 +211,7 @@ class MasterSlaveConnection(BaseObject): .. seealso:: :meth:`end_request` .. versionadded:: 2.8 """ - self.disconnect() + self._disconnect() def set_cursor_manager(self, manager_class): """Set the cursor manager for this connection. diff --git a/pymongo/mongo_client.py b/pymongo/mongo_client.py index 406f8b3fc..da5b00729 100644 --- a/pymongo/mongo_client.py +++ b/pymongo/mongo_client.py @@ -1089,7 +1089,7 @@ class MongoClient(common.BaseObject): warnings.warn("disconnect is deprecated in this version of PyMongo " "and removed in PyMongo 3. Use close() instead.", DeprecationWarning, stacklevel=2) - self.close() + self._disconnect() def close(self): """Disconnect from MongoDB. @@ -1103,6 +1103,10 @@ class MongoClient(common.BaseObject): .. seealso:: :meth:`end_request` .. versionadded:: 2.1 """ + self._disconnect() + + def _disconnect(self): + """Internal disconnect helper.""" self.__connecting_lock.acquire() member, self.__member = self.__member, None self.__connecting_lock.release() diff --git a/pymongo/mongo_replica_set_client.py b/pymongo/mongo_replica_set_client.py index bc139faea..568a35595 100644 --- a/pymongo/mongo_replica_set_client.py +++ b/pymongo/mongo_replica_set_client.py @@ -1439,6 +1439,10 @@ class MongoReplicaSetClient(common.BaseObject): """Disconnect from the replica set primary, unpin all members, and refresh our view of the replica set. """ + self._disconnect() + + def _disconnect(self): + """Internal disconnect helper.""" rs_state = self.__rs_state if rs_state.primary_member: rs_state.primary_member.reset() @@ -1523,7 +1527,7 @@ class MongoReplicaSetClient(common.BaseObject): assert response["number_returned"] == 1 result = response["data"][0] - helpers._check_command_response(result, self.disconnect) + helpers._check_command_response(result, self._disconnect) # write commands - skip getLastError checking if is_command: @@ -1534,7 +1538,7 @@ class MongoReplicaSetClient(common.BaseObject): if error_msg is None: return result if error_msg.startswith("not master"): - self.disconnect() + self._disconnect() raise AutoReconnect(error_msg) code = result.get("code") @@ -1645,7 +1649,7 @@ class MongoReplicaSetClient(common.BaseObject): except(ConnectionFailure, socket.error), why: member.discard_socket(sock_info) if _connection_to_use in (None, -1): - self.disconnect() + self._disconnect() raise AutoReconnect(str(why)) except: sock_info.close() @@ -1758,7 +1762,7 @@ class MongoReplicaSetClient(common.BaseObject): except AutoReconnect: if _connection_to_use in (-1, rs_state.writer): # Primary's down. Refresh. - self.disconnect() + self._disconnect() raise # To provide some monotonic consistency, we use the same member as @@ -1780,7 +1784,7 @@ class MongoReplicaSetClient(common.BaseObject): self.__try_read(pinned_member, msg, **kwargs)) except AutoReconnect, why: if _must_use_master or mode == ReadPreference.PRIMARY: - self.disconnect() + self._disconnect() raise else: errors.append(str(why))