Databases 7 min read

Understanding the Difference Between testWhileIdle and keepAlive in Druid for Dble Connections

This article explains how the testWhileIdle and keepAlive parameters of the Druid connection pool differ in their idle‑connection validation behavior, demonstrates the effects with a Java test program, and recommends using keepAlive for reliable Dble connectivity.

Aikesheng Open Source Community
Aikesheng Open Source Community
Aikesheng Open Source Community
Understanding the Difference Between testWhileIdle and keepAlive in Druid for Dble Connections

When using the Druid connection pool with Dble, users often encounter intermittent CommunicationsException errors caused by operating on connections that have already been closed. To mitigate this, enabling idle‑connection detection in Druid is recommended.

Druid provides two related configuration options introduced in version 1.0.28: testWhileIdle and keepAlive . Although both aim to validate idle connections, they operate in fundamentally different ways.

Conclusion

Enabling testWhileIdle causes Druid to validate a connection only when the connection is borrowed from the pool; it does not perform periodic checks during the timeBetweenEvictionRunsMillis interval.

Enabling keepAlive makes Druid run validation queries at the interval defined by timeBetweenEvictionRunsMillis , actively checking idle connections.

In practice, it is advisable to enable keepAlive for regular idle‑connection validation, as its semantics differ from those of other pools such as DBCP, and misuse can lead to unexpected behavior.

Detailed Test Procedure

The test program initializes a Druid pool with a single idle connection, sets timeBetweenEvictionRunsMillis to 10 seconds, and prints the pool’s state before and after a 12‑second pause. By comparing the LastActiveTime of the connection in the two dumps, we can infer whether validation occurred.

Test program:

public static void main(String[] args) {
try {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mysql://127.0.0.1:8066/testdb2?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8");
dataSource.setUsername("root");
dataSource.setPassword("123456");
// Enable testWhileIdle
// dataSource.setTestWhileIdle(true);
// Enable keepAlive
dataSource.setKeepAlive(true);
// Set eviction interval to 10s
dataSource.setTimeBetweenEvictionRunsMillis(10 * 1000);
dataSource.setMinEvictableIdleTimeMillis(5000);
dataSource.setValidationQuery("select 'x'");
dataSource.setMinIdle(1);
dataSource.setMaxActive(1);
Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement("select 1");
ps.close();
conn.close();
// First dump
System.out.println(dataSource.dump());
Thread.sleep(12000);
// Second dump
System.out.println(dataSource.dump());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}

Results with keepAlive enabled

First dump:

{
CreateTime:"2021-03-23 18:46:59",
ActiveCount:0,
PoolingCount:1,
CreateCount:1,
DestroyCount:0,
CloseCount:1,
ConnectCount:1,
Connections:[{ID:515132998, ConnectTime:"2021-03-23 18:46:59", UseCount:1, LastActiveTime:"2021-03-23 18:47:00"}]
}

Second dump (after 12 s):

{
CreateTime:"2021-03-23 18:46:59",
ActiveCount:0,
PoolingCount:1,
CreateCount:1,
DestroyCount:0,
CloseCount:1,
ConnectCount:1,
Connections:[{ID:515132998, ConnectTime:"2021-03-23 18:46:59", UseCount:1, LastActiveTime:"2021-03-23 18:47:09"}]
}

The LastActiveTime changed from 18:47:00 to 18:47:09, confirming that a validation query was executed during the 10‑second interval.

Results with testWhileIdle enabled

First dump:

{
CreateTime:"2021-03-23 18:52:38",
ActiveCount:0,
PoolingCount:1,
CreateCount:1,
DestroyCount:0,
CloseCount:1,
ConnectCount:1,
Connections:[{ID:515132998, ConnectTime:"2021-03-23 18:52:39", UseCount:1, LastActiveTime:"2021-03-23 18:52:39"}]
}

Second dump (after 12 s):

{
CreateTime:"2021-03-23 18:52:38",
ActiveCount:0,
PoolingCount:1,
CreateCount:1,
DestroyCount:0,
CloseCount:1,
ConnectCount:1,
Connections:[{ID:515132998, ConnectTime:"2021-03-23 18:52:39", UseCount:1, LastActiveTime:"2021-03-23 18:52:39"}]
}

Here the LastActiveTime remained unchanged, indicating that no validation occurred during the interval.

These observations confirm that keepAlive performs periodic idle‑connection checks, while testWhileIdle only validates connections at borrow time.

JavaDatabaseConnection PoolDruidkeepalivetestWhileIdle
Aikesheng Open Source Community
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.