3

I have a Django app that has several database backends - all connected to different instances of Postgresql database. One of them is not guaranteed to be always online. It even can be offline when application starts up.

Can I somehow configure Django to use lazy connections? I would like to:

  • Try querying
  • return "sorry, try again later" if database is offline
  • or return the results if database is online

Is this possible?

2
  • What do you mean by "sorry, try again later" you want an exception/ just to always return null results? Commented Jan 2, 2014 at 8:59
  • yeah, catching exception in code and report to use with either some message or empty results. Commented Jan 2, 2014 at 9:18

1 Answer 1

3

The original confusion is that Django tries to connect to its databases on startup. This is actually not true. Django does not connect to database, until some app tries to access the database.

Since my web application uses auth and site apps, it looks like it tries to connect on startup. But its not tied to startup, its tied to the fact that those app access the database "early".

If one defines second database backend (non-default), then Django will not try connecting to it unless application tries to query it.

So the solution was very trivial - originally I had one database that hosted both auth/site data and also "real" data that I've exposed to users. I wanted to make "real" database connection to be volatile. So I've defined separate psql backend for it and switched default backend to sqlite.

Now when trying to access "real" database through Query, I can easily wrap it with try/except and handle "Sorry, try again later" over to the user.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.