Mongo clients with the same settings should be shared since they manage a connection pool.
Also, I removed old code that was supposed to support Pymongo<2.1 which we don't support anymore.
This commit is contained in:
parent
f099dc6a37
commit
29309dac9a
@ -93,20 +93,11 @@ def get_connection(alias=DEFAULT_CONNECTION_NAME, reconnect=False):
|
|||||||
raise ConnectionError(msg)
|
raise ConnectionError(msg)
|
||||||
conn_settings = _connection_settings[alias].copy()
|
conn_settings = _connection_settings[alias].copy()
|
||||||
|
|
||||||
if hasattr(pymongo, 'version_tuple'): # Support for 2.1+
|
conn_settings.pop('name', None)
|
||||||
conn_settings.pop('name', None)
|
conn_settings.pop('slaves', None)
|
||||||
conn_settings.pop('slaves', None)
|
conn_settings.pop('is_slave', None)
|
||||||
conn_settings.pop('is_slave', None)
|
conn_settings.pop('username', None)
|
||||||
conn_settings.pop('username', None)
|
conn_settings.pop('password', None)
|
||||||
conn_settings.pop('password', None)
|
|
||||||
else:
|
|
||||||
# Get all the slave connections
|
|
||||||
if 'slaves' in conn_settings:
|
|
||||||
slaves = []
|
|
||||||
for slave_alias in conn_settings['slaves']:
|
|
||||||
slaves.append(get_connection(slave_alias))
|
|
||||||
conn_settings['slaves'] = slaves
|
|
||||||
conn_settings.pop('read_preference', None)
|
|
||||||
|
|
||||||
connection_class = MongoClient
|
connection_class = MongoClient
|
||||||
if 'replicaSet' in conn_settings:
|
if 'replicaSet' in conn_settings:
|
||||||
@ -119,7 +110,19 @@ def get_connection(alias=DEFAULT_CONNECTION_NAME, reconnect=False):
|
|||||||
connection_class = MongoReplicaSetClient
|
connection_class = MongoReplicaSetClient
|
||||||
|
|
||||||
try:
|
try:
|
||||||
_connections[alias] = connection_class(**conn_settings)
|
connection = None
|
||||||
|
connection_settings_iterator = ((alias, settings.copy()) for alias, settings in _connection_settings.iteritems())
|
||||||
|
for alias, connection_settings in connection_settings_iterator:
|
||||||
|
connection_settings.pop('name', None)
|
||||||
|
connection_settings.pop('slaves', None)
|
||||||
|
connection_settings.pop('is_slave', None)
|
||||||
|
connection_settings.pop('username', None)
|
||||||
|
connection_settings.pop('password', None)
|
||||||
|
if conn_settings == connection_settings and _connections.get(alias, None):
|
||||||
|
connection = _connections[alias]
|
||||||
|
break
|
||||||
|
|
||||||
|
_connections[alias] = connection if connection else connection_class(**conn_settings)
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
raise ConnectionError("Cannot connect to database %s :\n%s" % (alias, e))
|
raise ConnectionError("Cannot connect to database %s :\n%s" % (alias, e))
|
||||||
return _connections[alias]
|
return _connections[alias]
|
||||||
|
@ -34,6 +34,17 @@ class ConnectionTest(unittest.TestCase):
|
|||||||
conn = get_connection('testdb')
|
conn = get_connection('testdb')
|
||||||
self.assertTrue(isinstance(conn, pymongo.mongo_client.MongoClient))
|
self.assertTrue(isinstance(conn, pymongo.mongo_client.MongoClient))
|
||||||
|
|
||||||
|
def test_sharing_connections(self):
|
||||||
|
"""Ensure that connections are shared when the connection settings are exactly the same
|
||||||
|
"""
|
||||||
|
connect('mongoenginetest', alias='testdb1')
|
||||||
|
|
||||||
|
expected_connection = get_connection('testdb1')
|
||||||
|
|
||||||
|
connect('mongoenginetest', alias='testdb2')
|
||||||
|
actual_connection = get_connection('testdb2')
|
||||||
|
self.assertIs(expected_connection, actual_connection)
|
||||||
|
|
||||||
def test_connect_uri(self):
|
def test_connect_uri(self):
|
||||||
"""Ensure that the connect() method works properly with uri's
|
"""Ensure that the connect() method works properly with uri's
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user