I use MySQL in my ASP.NET app (with the MySQL .NET Connector), is there anyway to programmatically check whether the db server is down? I came across MySQLConnection.Ping() but unfortunately that doesn't work the way it should (the connection has to be opened first using Open(), also it keeps on returning false even if the DB Server becomes available again). The only solution I came up with is to use the Open() method, if that throws an exception then the DB server is not available, is there a more reliable way to do this? I read somewhere that using Open() might not work as expected if connection pooling is used, this could result an exception being thrown even if the DB server is up.
If you want the details please read on:
In my app, I log all exceptions (first I try to log them to the DB Server - could be the same DB Server that's down but could be not, depending on the configuration - if that fails the exceptions are logged to a text file). Now I want to handle the case if the DB server is down, normally what will happen is that exceptions will keep on being thrown with almost every request to my app, this means the log will be full with those exceptions, the other problem is that I'll keep on serving error pages to users (or in other words the users will keep on seeing error pages with every request they make to the app), what I want to do is that if a db exception is thrown (ie. of type MySQLException) to check whether the DB server is up, if not then change some flag in my app and serve a page that contains something like the application is temporarily unavailable (the flag is checked in an httpModule and redirects or transfers to that page). I'm also planning to make it send me an SMS using an SMS proxy so that I could take an action, but this another story and is not related to this question.