如何在启动时预初始化DBCP连接池? [英] How to preinitialize DBCP connection pool on startup?
问题描述
我的项目设置是 -
The setup of my project is -
- Spring JDBC for persistence
- Apache DBCP 1.4用于连接池
- Linux上的Mysql 5
这是我的应用程序捕获的日志与数据库的交互。
Here is the log of my application that captures the interactions with the database.
2013-01-29 15:52:21,549 DEBUG http-bio-8080-exec-3 org.springframework.jdbc.core.JdbcTemplate - Executing SQL query [SELECT id from emp]
2013-01-29 15:52:21,558 DEBUG http-bio-8080-exec-3 org.springframework.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
2013-01-29 15:52:31,878 INFO http-bio-8080-exec-3 jdbc.connection - 1. Connection opened org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
2013-01-29 15:52:31,878 DEBUG http-bio-8080-exec-3 jdbc.connection - open connections: 1 (1)
2013-01-29 15:52:31,895 INFO http-bio-8080-exec-3 jdbc.connection - 1. Connection closed org.apache.commons.dbcp.DelegatingConnection.close(DelegatingConnection.java:247)
2013-01-29 15:52:31,895 DEBUG http-bio-8080-exec-3 jdbc.connection - open connections: none
2013-01-29 15:52:41,950 INFO http-bio-8080-exec-3 jdbc.connection - 2. Connection opened org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
2013-01-29 15:52:41,950 DEBUG http-bio-8080-exec-3 jdbc.connection - open connections: 2 (1)
2013-01-29 15:52:52,001 INFO http-bio-8080-exec-3 jdbc.connection - 3. Connection opened org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
2013-01-29 15:52:52,002 DEBUG http-bio-8080-exec-3 jdbc.connection - open connections: 2 3 (2)
2013-01-29 15:53:02,058 INFO http-bio-8080-exec-3 jdbc.connection - 4. Connection opened org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
2013-01-29 15:53:02,058 DEBUG http-bio-8080-exec-3 jdbc.connection - open connections: 2 3 4 (3)
2013-01-29 15:53:03,403 DEBUG http-bio-8080-exec-3 org.springframework.jdbc.core.BeanPropertyRowMapper - Mapping column 'id' to property 'id' of type int
2013-01-29 15:53:04,494 DEBUG http-bio-8080-exec-3 org.springframework.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource
日志中有两件事情是清楚的 -
Two things are clear from the log -
- 当收到第一个执行查询的请求时,连接池才开始创建连接。
- 4个连接的池接近30个初始化的秒数。
我的问题是 -
- 应该如何配置DBCP在启动时自动初始化?
- 创建连接真的需要很长时间吗?
- How should one configure DBCP to initialize on startup automatically?
- Should it really take that long to create connections?
注意:请不要建议切换到C3P0或Tomca连接池。我知道那些解决方案。我更感兴趣的是了解手头的问题,而不仅仅是快速修复。
此外我确信DBCP也可以这么基本。
dbcontext的内容 -
Contents of dbcontext -
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.jdbc.url}" />
<property name="username" value="${db.user}" />
<property name="password" value="${db.password}" />
<property name="maxActive" value="20" />
<property name="initialSize" value="4" />
<property name="testOnBorrow" value="true" />
<property name="validationQuery" value="SELECT 1" />
</bean>
推荐答案
在您第一次请求之前,initialSize不会生效一个连接。从java文档到 BasicDataSource #setInitialSize
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.
注意:一旦池初始化为
,此方法当前无效。第一次调用
以下方法之一时初始化池:getConnection,setLogwriter,
setLoginTimeout,getLoginTimeout,getLogWriter。
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.
尝试将 init-method =getLoginTimeout
添加到您的bean中以确认这一点。
Try adding init-method="getLoginTimeout"
to your bean to confirm this.
这篇关于如何在启动时预初始化DBCP连接池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!