0

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

2
  • Are you not able to change the mySQL config? Commented Oct 16, 2009 at 12:49
  • maybe you mean "query string" instead of "connection string"? Commented Oct 16, 2009 at 12:50

1 Answer 1

1

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.)

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

1 Comment

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

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.