验证 Apache Commons Pool 2 中的空闲对象的时间间隔是多少
What's interval to validate the idle object in Apache Commons Pool 2
我正在使用Apache Commons Pool 2
,我已经将配置testWhileIdle
设置为true,这意味着Pool框架将检查空闲对象是否有效。
我会问这个检查是否定期进行,如果是,默认间隔是多少,我如何更改它。
我这么问是因为池中的对象应该定期连接到服务器以保持活动状态,我认为池的 validateObject
方法是实现此目的的好地方。
根据@user7294900 的评论,有两个配置参数:
timeBetweenEvictionRunsMillis
,默认值为-1
minEvictableIdleTimeMillis
,默认值为1800*60*30
我有以下两个问题:
- 这两个参数有什么区别?
- 我都设置为5000,池中有8个对象,我在PooledObjectFactory的
validateObject
方法中打印了对象的hashcode,发现只打印了3个对象的hashcode ,然后不再打印更多消息,看起来 validateObject
不是每 5 秒调用一次,或者对 validateObject
的调用卡住了?
问题解决了,请看评论
您可以use\update timeBetweenEvictionRunsMillis
更改空闲验证时间,请参阅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).
我正在使用Apache Commons Pool 2
,我已经将配置testWhileIdle
设置为true,这意味着Pool框架将检查空闲对象是否有效。
我会问这个检查是否定期进行,如果是,默认间隔是多少,我如何更改它。
我这么问是因为池中的对象应该定期连接到服务器以保持活动状态,我认为池的 validateObject
方法是实现此目的的好地方。
根据@user7294900 的评论,有两个配置参数:
timeBetweenEvictionRunsMillis
,默认值为-1minEvictableIdleTimeMillis
,默认值为1800*60*30
我有以下两个问题:
- 这两个参数有什么区别?
- 我都设置为5000,池中有8个对象,我在PooledObjectFactory的
validateObject
方法中打印了对象的hashcode,发现只打印了3个对象的hashcode ,然后不再打印更多消息,看起来validateObject
不是每 5 秒调用一次,或者对validateObject
的调用卡住了?
问题解决了,请看评论
您可以use\update timeBetweenEvictionRunsMillis
更改空闲验证时间,请参阅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).