title: In Django, how can I set db connection timeout?
body: <p>OK, I know it's not that simple. I have two db connections defined in my settings.py: <code>default</code> and <code>cache</code>. I'm using <code>DatabaseCache</code> backend from <code>django.core.cache</code>. I have database router defined so I can use separate database/schema/table for my models and for cache. Perfect!</p>
  
  <p>Now sometimes my cache DB is not available and there are two cases:</p>
  
  <ol>
  <li><p>Connection to databse was established already when DB crashed - this is easy - I can use this recipe: <a href="http://code.activestate.com/recipes/576780-timeout-for-nearly-any-callable/" rel="nofollow noreferrer">http://code.activestate.com/recipes/576780-timeout-for-nearly-any-callable/</a> and wrap my query like this:</p>
  
  <pre><code>try:
      timelimited(TIMEOUT, self._meta.cache.get, cache_key))
  expect TimeLimitExprired:
      # live without cache
  </code></pre></li>
  <li><p>Connection to database wasn't yet established - so I need to wrap in <code>timelimited</code> 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 only <code>cache</code> connection, leave <code>default</code> connection without timeout)</p></li>
  </ol>
  
  <p>Do you know how to do point 2?</p>
  
  <p>Please note, this answer <a href="https://stackoverflow.com/a/1084571/940208">https://stackoverflow.com/a/1084571/940208</a> is not correct:</p>
  
  <pre><code>grep -R "connect_timeout" /usr/local/lib/python2.7/dist-packages/django/db
  </code></pre>
  
  <p>gives no results and cx_Oracle driver doesn't support this parameter as far as I know.</p>