Multiple use of connect on the same alias silently fail
>>> db1 = mongoengine.connect('db1')
>>> db2 = mongoengine.connect('db2')
>>> db1
MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True, read_preference=Primary())
>>> db2
MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True, read_preference=Primary())
>>> db1 is db2
True
If disconnect is not used before, calling connect on an alias which has already been subject to a connect call results in returning the previous connection, leading to a silent fail if the 2nd connect call contained different connection informations.
I see two possibilities to solve this:
- Check the parameters given the the connect to determine if the connection must be reconfigured
- If
connectis called on an alias already configured, aConnectionErroris raised saying the user must firt usedisconnecton the alias before being able to reconfigure this alias
I personally prefer the 2nd solution: checking connection parameters is complicated and risk to add subtle bugs (i.g. forgetting to check an exotic connection argument). On top of that zen of python told us that "explicit is better than implicit".
But using disconnect doesn't work either.
Currently working on this and fixing a few other things around connect/disconnect