I have a pool of MySQL connections for a web-based data service. When it starts to service a request, it takes a connection from the pool to use. The problem is that if there has been a significant pause since that particular connection has been used, the server may have timed it out and closed its end. I'd like to be able to detect this in the pool management code.
The trick is this: The environment in which I'm coding gives me only a very abstact API into the connection. I can basically only execute SQL statements. I don't have access to the actual socket or direct access to the MySQL client API.
So, the question is: What is the cheapest MySQL statement I can execute on the connection to determine if it is working. For example SELECT 1;
should work, but I'm wondering if there is something even cheaper? Perhaps something that doesn't even go across the wire, but is handled in the MySQL client lib and effectively answers the same question?
Clarification: I'm not concerned about checking if the MySQL server is running, or if it's database configuration is up enough to answer queries. If those things are down, then the subsequent SQL the service executes will get and handle the appropriate error. I'm only really concerned with if the TCP connection is open… since if the server closed it, then the web service's SQL will get an error that means "just reconnect and try again", and that would be inconvenient to do once down in the muck of the service code.
Closure: The /* ping */
hack is exactly the sort of thing I was looking for, but alas is only available via JDBC. Reading through the docs for that hack, it is clear it was put there for exactly the same reason I wanted it. For the curious, I'm working in Haskel, using HDBC and HDBC-mysql. I'm going to ask the author of HDBC-mysql to add a way to call mysql_ping()
either directly or via a similar hack.
Vlad's DO 1
was also the kind of thing I was after, and since the other hack isn't available outside of JDBC, I'll be using it.
Thanks for all the great discussion, especially @Vlad!