开发者

Django - Custom SQL in the connection string

开发者 https://www.devze.com 2022-12-08 12:25 出处:网络
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

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 so开发者_JAVA百科me 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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消