Understanding Druid Connection Validation Mechanisms in MySQL
This article explains how Alibaba's Druid JDBC pool validates MySQL connections, compares the default com_ping method with the validationQuery approach, shows configuration pitfalls, provides Java code examples, and demonstrates verification using tcpdump and MySQL general logs.
Druid is an open‑source JDBC connection pool from Alibaba that offers extensive MySQL support, including monitoring, password encryption, and SQL logging. When performing connection health checks, Druid can use two mechanisms: the default com_ping method and a custom validationQuery query.
The relevant configuration parameters are druid.validationQuery = select 1 (or any simple SELECT) and druid.mysql.usePingMethod = false to disable the built‑in ping and enable the validation query.
To verify the com_ping method, the author captured network traffic with tcpdump and observed MySQL protocol Ping (14) packets.
For the validationQuery method, the article provides the source of MySqlValidConnectionChecker, showing how Druid reflects into the MySQL connection class to invoke pingInternal when usePingMethod is true, and otherwise executes the configured SELECT statement via JDBC.
public MySqlValidConnectionChecker(){
try {
clazz = Utils.loadClass("com.mysql.jdbc.MySQLConnection");
if (clazz == null) {
clazz = Utils.loadClass("com.mysql.cj.jdbc.ConnectionImpl");
}
if (clazz != null) {
ping = clazz.getMethod("pingInternal", boolean.class, int.class);
}
if (ping != null) {
usePingMethod = true;
}
} catch (Exception e) {
LOG.warn("Cannot resolve com.mysql.jdbc.Connection.ping method. Will use 'SELECT 1' instead.", e);
}
configFromProperties(System.getProperties());
}
@Override
public void configFromProperties(Properties properties) {
String property = properties.getProperty("druid.mysql.usePingMethod");
if ("true".equals(property)) {
setUsePingMethod(true);
} else if ("false".equals(property)) {
setUsePingMethod(false);
}
}
public boolean isValidConnection(Connection conn, String validateQuery, int validationQueryTimeout) throws Exception {
if (conn.isClosed()) {
return false;
}
if (usePingMethod) {
// invoke pingInternal via reflection
ping.invoke(conn, true, validationQueryTimeout * 1000);
return true;
}
String query = (validateQuery == null || validateQuery.isEmpty()) ? DEFAULT_VALIDATION_QUERY : validateQuery;
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
if (validationQueryTimeout > 0) {
stmt.setQueryTimeout(validationQueryTimeout);
}
rs = stmt.executeQuery(query);
return true;
} finally {
JdbcUtils.close(rs);
JdbcUtils.close(stmt);
}
}The author discovered that the original program loaded Druid configuration from a file but did not copy those properties into System.getProperties(), causing usePingMethod to remain true and preventing the validation query from being used. The fix loads the properties file into the system properties before initializing the datasource.
// Original constructor
public DruidDriverTest() {
logger = Logger.getLogger("druid_driver_test");
this.dataSource = new DruidDataSource();
this.configPath = "./config.properties";
// ...
}
// Modified constructor
public DruidDriverTest() {
logger = Logger.getLogger("druid_driver_test");
this.configPath = "config.properties";
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(configPath))) {
System.getProperties().load(bufferedReader);
} catch (IOException e) {
e.printStackTrace();
return;
}
// continue initializing datasource
}Running the demo with Maven shows the datasource initialization logs, confirming that the validation query is set to SELECT 1 and that the connection pool behaves as expected. The MySQL general log further confirms that Druid issues the expected SELECT statements during health checks.
Readers are invited to share their own experiences with Druid validation or other parameters.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Aikesheng Open Source Community
The Aikesheng Open Source Community provides stable, enterprise‑grade MySQL open‑source tools and services, releases a premium open‑source component each year (1024), and continuously operates and maintains them.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
