views:

112

answers:

1

Hello, I have had some issues with downtime as a result of hitting the max_user_connections limit in MySql.

The default connection timeout is 8 hours, so once we hit the limit (and having no access to kill the connections on our shared hosting) I simply had to wait 8 hours for the connections to time out.

I would like to add the following code to my connection string:

SET wait_timeout=300;

Which would change the timeout to 5 minutes. As you can imagine, I'm much happier to deal with 5 minutes of downtime than 8 hours. ;)

Is there a good way of adding custom SQL to the connection string in django?

If not, it has been suggested that we write some middleware that runs the SQL before the view is processed.

That might work, but I would feel more comfortable knowing that the query was absolutely guaranteed to run for every connection, even if more than one connection is opened for each view.

Thanks!

PS - before you tell me I should just hunt down the code that is keeping the connections from being closed - Never Fear! - we are doing that, but I would like to have this extra insurance against another 8 hour block of downtime

+1  A: 

You can specify a list of commands to send to MySQL when the connection is open, by setting the DATABASE_OPTIONS dictionary in settings.py.

(Incidentally, note that Django doesn't open a new connection for every view.)

Daniel Roseman
Good info! thanks so much this is exactly the solution I was hoping for :)
Jiaaro