DBCP何时初始化的一个小坑

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/haolexiao/article/details/79468654

因为目前的项目要频繁获取数据库连接,发现这块已经成为了时间瓶颈,所以考虑可以引入数据库连接池。查了下参考资料,考虑采用DBCP这种数据库连接池。

但是实际上使用的时候,发现在程序首次去获取数据库连接池的连接时,时间还是很长,看了下时间发现应该就是数据连接池初始化和建立连接的时间。
当时就非常疑惑,难道在设置初始化的时候,数据库连接池没有建立连接吗?
google搜了官方说明文档,在setInitialSize这个函数说明下面有一个Note:

Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first time one of the following methods is invoked: getConnection, setLogwriter, setLoginTimeout, getLoginTimeout, getLogWriter.

这里就明确说了,在设置DBCP连接池的初始化参数的时候,并不会导致初始化,而是当数据库连接池第一次调用getConnection, setLogwriter, setLoginTimeout, getLoginTimeout, getLogWriter这些函数的时候才进行初始化。

所以要想第一次连接就已经完成初始化的状态的话,可以考虑在参数设置完DBCP之后,就调用getConnection()一下,然后再关掉,或者调用setLogwriter()这些函数。

猜你喜欢

转载自blog.csdn.net/haolexiao/article/details/79468654
今日推荐