mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

Lazy connection

Open rozza opened this issue 14 years ago • 3 comments

From the mailing list:

Hi All,

I am running django + mongo. So I have connect() call in the settings.py. However, if mongodb is down (yeah, it may happen with dev setup), the webapps are totally unavailable. A large portion of our site doesn't require mongodb to run. Is there a way to config mongoengine do lazy connection?
For example, call lazy_connect() with parameter, and don't connect to db until necessary.

Thanks & enjoy weekend! Frank

rozza avatar Sep 15 '11 07:09 rozza

This connect() call in settings.py is a PITA. Is there an intention to replace it with a standard settings dict?

andreif avatar Feb 13 '12 12:02 andreif

+1, I need to launch my web application just to re-generate static assets and it requires MongoDB only because it connects non-lazily.

Any workarounds available?

honzajavorek avatar Dec 05 '13 17:12 honzajavorek

Found it! Instead of:

mongoengine.connect(**conn_settings)

...it is possible to write:

mongoengine.register_connection(
    mongoengine.DEFAULT_CONNECTION_NAME,
    conn_settings.pop('db', None),
    **conn_settings
)

...which then results in lazy connecting.

honzajavorek avatar Dec 05 '13 18:12 honzajavorek