javaapache-commons-pool

What's interval to validate the idle object in Apache Commons Pool 2


I am using Apache Commons Pool 2, I have set the configuration testWhileIdle to true, which means that the Pool framework will check whether the idle objects they are valid.

I would ask whether this check occurs periodically, if so, what's the default interval, and how i can change it.

I ask so because the objects in the pool should periodically connect to the server to keep alive, and I think pool's validateObject method is a good place to fulfill this.

Per @user7294900's comments,there are two configuration parameters:

  1. timeBetweenEvictionRunsMillis, default value is -1
  2. minEvictableIdleTimeMillis, default value is 1800*60*30

I have following two question:

  1. What's the difference between these two parameters?
  2. I set both of them to 5000, and I have 8 objects in the pool, I print the object's hashcode in my PooledObjectFactory's validateObject method, I found that only 3 objects'hash code has been printed out, then there is no more messages printed again, looks the validateObject 's is not called every 5 seconds or the call to validateObject is stuck?

I figured out the problem, please see the comments


Solution

  • You can use\update timeBetweenEvictionRunsMillis for changing time of idle validation, see Tomcat JDBC Connection Pool.

    timeBetweenEvictionRunsMillis (int) The number of milliseconds to sleep between runs of the idle connection validation/cleaner thread. This value should not be set under 1 second. It dictates how often we check for idle, abandoned connections, and how often we validate idle connections. The default value is 5000 (5 seconds).