-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
I have an issue accessing multiple databases allocated in the same host one after another.
The thing is that I want to retrieve two different types of documents from two different databases with different authentication settings.
I have set up this example code.
from docs.doc1 import Doc1
from docs.doc2 import Doc2
from mongoengine import connect
db1_connection_dict = dict(
alias='db1',
db='db1',
host='',
port=27017,
username='db1',
password='db1',
authentication_source='admin',
)
db2_connection_dict = dict(
alias='db2',
db='db2',
host='',
port=27017,
username='db2',
password='db2',
authentication_source='admin',
)
Doc1._meta['db_alias'] = 'db1'
Doc2._meta['db_alias'] = 'db2'
connect(**db1_connection_dict)
connect(**db2_connection_dict)
read1_db1 = Doc1.objects().first() # This works
read1_db2 = Doc2.objects().first() # This works
read2_db1 = Doc1.objects().first() # This crashes
I get an error as if the authorization wasn’t right.
I have inquired into this matter and the problem is that when it tries to query db1 for the second time, it is trying to access it with the db2 authentication settings.
I have realized that by setting up a breakpoit before the second query to the first database and running connectionStatus command in the db1 database:
import mongoengine
mongoengine.connections._dbs[‘db1’].command(‘connectionStatus’)
And I get
{'authInfo': {'authenticatedUsers': [{'user': 'db2', 'db': 'admin'}], 'authenticatedUserRoles': [{'role': 'read', 'db': 'db2'}]}, 'ok': 1.0}
Which are the db2 authentication settings.
If anyone could shed some light over this I would really appreciate it.
Thanks!