How to preinitialize DBCP connection pool on startup?

自闭症网瘾萝莉.ら 提交于 2019-12-04 08:27:54

The initialSize doesn't take effect until you first request a connection. From the java docs to BasicDataSource#setInitialSize

Sets the initial size of the connection pool.

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.

Try adding init-method="getLoginTimeout" to your bean to confirm this.

For a web application, you can implement ServletContextListener.contextInitialized() method and fire a test query (e.g. Select ID From Emp Limit 1) using your DataAccess layer. This should initialize your connection pool and make it ready before your application starts serving real user from web.

Have a look at the initialSize property -especially the part about when the pool is initialized. As sbridges points out, you can use the init-method property on beans to call one of the methods to trigger pool creation.

Also, you should look into why it takes 7.5 seconds on average to create a connection...

Cheers,

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!