mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

Multiple use of connect on the same alias silently fail

Open touilleMan opened this issue 10 years ago • 2 comments

>>> 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 connect is called on an alias already configured, a ConnectionError is raised saying the user must firt use disconnect on 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".

touilleMan avatar Jan 19 '16 14:01 touilleMan

But using disconnect doesn't work either.

Fierozen avatar Dec 19 '18 20:12 Fierozen

Currently working on this and fixing a few other things around connect/disconnect

bagerard avatar Apr 22 '19 13:04 bagerard