• Home
  • About

szehau's weblog

Life, Internet, Software, Gadgets, Programming and Investments

  • life log
  • internet
  • programming
  • software
  • working holiday
  • gadget
  • project

Enhanced WordPress DB class

I frequently see WordPress blogs encounter mysql connection lost or exceeded user connections limit error when I’m browsing their websites. This problem normally happens on a WordPress blog that hosts on a shared hosting, especially those busy server where too many users sit on the same server.

I have modified the WordPress’s DB class to put in some enhancements to reduce the occurrence of the error.

  • Retry to open mysql connection when the connection is failed to open at the first time.
  • Reopen the connection during failure (on connection lost error) when calling mysql_query() (for SELECT statements only).
  • Currently the code will retry for 5 times and interval for next retry is 200 milliseconds.

To modify the number of retries and interval, you simply need to modify the following lines of code:

$this->retry_max = 5; // Maximum retry 5 times
$this->retry_interval = 200; // 200 miliseconds

I have installed this script on my blog and so far don’t see any problem. If you are interested to have this code, you can download it from here:

Enhanced WordPress DB class

Do let me know if you found any bug in the code.

Notes:

  • Use this script at your own risk!
  • The WordPress DB class is from WordPress version 2.2.1. I’m not sure if it is compatible with the older versions.
  • Remember to backup your original source file.

It is not recommended to tweak the total retry time to more than 5 seconds (current setting is 5 x 200ms = 1 second). If you still owest getting the connection lost or exceeded maximum user connections limit (User xxx already has more than ‘max_user_connections’ active connections) error, you might need to ask you web hosting company to change the connection limit or consider to host your website on a better server.

About Sze Hau

Geek. Love programming. Coffee addicted. Married with a child. Working towards financial freedom.

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

More Posts

5 Essential Things You Need To Have At Your Home During The COVID-19 Pandemic

June 1, 2021 22:15 By Sze Hau

LED Downlight

Troubleshooting A Flickering LED Downlight

February 13, 2016 00:29 By Sze Hau

Repair A Broken iPhone Lightning Cable

January 30, 2016 00:54 By Sze Hau

Blog Network

  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • Snippet IT Information technology news, ideas and experiences and opinions