OK, I know it's not that simple. I have two db connections defined in my settings.py: default
and cache
. I'm using DatabaseCache
backend from django.core.cache
. I have database router defined so I can use separate database/schema/table for my models and for cache. Perfect!
Now sometimes my cache DB is not available and there are two cases:
Connection to databse was established already when DB crashed - this is easy - I can use this recipe: http://code.activestate.com/recipes/576780-timeout-for-nearly-any-callable/ and wrap my query like this:
try: timelimited(TIMEOUT, self._meta.cache.get, cache_key)) expect TimeLimitExprired: # live without cache
Connection to database wasn't yet established - so I need to wrap in
timelimited
some portion of code that actually establishes database connection. But I don't know where such code exists and how to wrap it selectively (i.e. wrap onlycache
connection, leavedefault
connection without timeout)
Do you know how to do point 2?
Please note, this answer https://stackoverflow.com/a/1084571/940208 is not correct:
grep -R "connect_timeout" /usr/local/lib/python2.7/dist-packages/django/db
gives no results and cx_Oracle driver doesn't support this parameter as far as I know.