How to initialize DBCP connection pool at startup?

Setting up my project -

  • Spring JDBC to save
  • Apache DBCP 1.4 for pooling
  • Mysql 5 on Linux

Here is the log of my application that captures database interactions.

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 can be seen from the magazine -

  • The connection pool is only starting to create connections when it receives the first request to execute the request.
  • A pool of 4 connections takes about 30 seconds to initialize.

My questions -

  • How to configure DBCP to automatically initialize at startup?
  • Should it take so long to create connections?

Note. Do not suggest switching to the C3P0 or Tomcat connection pool. I know about these solutions. I'm more interested in understanding the problem, not just a quick fix. Also, I'm sure there should be something basic with DBCP.

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>
+5
3

InitialSize . java docs BasicDataSource # setInitialSize

.

: , . : getConnection, setLogwriter, setLoginTimeout, getLoginTimeout, getLogWriter.

init-method="getLoginTimeout" bean, .

+6

- ServletContextListener.contextInitialized() (, ID Emp Limit 1), DataAccess. , .

+3

initialSize - , . sbridges, init-method beans, .

, , 7.5 ...

,

+2

All Articles