public SQLException isValidConnection(Connection c) {
//if there is a ping method then use it, otherwise just use a 'SELECT 1' statement
if (driverHasPingMethod)
{
try
{
Class mysqlConnection = Thread.currentThread().getContextClassLoader().loadClass("com.mysql.jdbc.Connection");
Method ping = mysqlConnection.getMethod("ping", new Class[] {});
ping.invoke(c, params);
}
catch (Exception e)
{
if (e instanceof SQLException)
{
return (SQLException) e;
}
else
{
log.warn("Unexpected error in ping", e);
return new SQLException("ping failed: " + e.toString());
}
}
}
else
{
Statement stmt = null;
ResultSet rs = null;
try
{
stmt = c.createStatement();
rs = stmt.executeQuery("SELECT 1");
}
catch (Exception e)
{
if (e instanceof SQLException)
{
return (SQLException) e;
}
else
{
log.warn("Unexpected error in ping (SELECT 1)", e);
return new SQLException("ping (SELECT 1) failed: " + e.toString());
}
}
finally
{
//cleanup the Statment
try
{
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
}
catch (SQLException ignore)
{
}
}
}
return null;
}
|