AgDude
AgDude

Reputation: 1185

Django multiple databases - can't connect to mysql server fallback to sqlite

I have a django applicaiton with multiple databases. The default database is on the local machine. There is also a remote mysql database which is used for some write operations, but it is not always up. When the server is down, mysqldb raises an OperationalError.

I would like have a local sqlite database called 'fallback' which would accept the data if the mysql server is down. I realize that this involves at try/except clause in django.db.mysql.base, but I am not quite sure where to go from there. Has anyone tried something similar? Do you have suggestions on a better way to handle this?

Upvotes: 2

Views: 841

Answers (1)

Josh Smeaton
Josh Smeaton

Reputation: 48730

You could probably use Database Routers in combination with a custom base Model class that overrides the save method. Wrap it in a try..catch, and if the OperationalError occurs, provide some hints so your database router can determine if the fallback needs to be used.

I think this will be the cleanest way, rather than modifying the django code itself.

Upvotes: 1

Related Questions