title: Datasource initialization at server start up
body: <p>We have an application where we have used spring for IOC. We have the dataSource bean configured in applicationContext.xml and that is referenced in other bean definations.</p>
  
  <p>The dataSource bean defination looks like:</p>
  
  <pre><code>    &lt;bean id="dbDataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close"&gt;
          &lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /&gt;
          &lt;property name="url"
              value="jdbc:oracle:oci:@TESTDB" /&gt;
          &lt;property name="username" value="TESTUSER" /&gt;
          &lt;property name="password" value="TESTPWD" /&gt;
          &lt;property name="initialSize" value="50" /&gt;
          &lt;property name="maxActive" value="40" /&gt;
          &lt;property name="maxIdle" value="10" /&gt;
          &lt;property name="minIdle" value="10" /&gt;
          &lt;property name="maxWait" value="-1" /&gt;
      &lt;/bean&gt;
      &lt;bean id="serviceDAO" class="com.test.impl.ServiceDAOImpl"&gt;
          &lt;property name="dataSource" ref="dbDataSource" /&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p>ServiceDAOImpl looks as follows:</p>
  
  <pre><code>public class ServiceDAOImpl implements ServiceDAO {
  
  
      private JdbcTemplate jdbcTemplate;
  
      public void setDataSource(DataSource dataSource) {
          this.jdbcTemplate = new JdbcTemplate(dataSource);
      }
      @SuppressWarnings({ "rawtypes", "unchecked" })
      public ValueObj readValue(String key) {
  
          String query = "SELECT * FROM SERVICE_LOOKUP WHERE KEY=?";
          /**
           * Implement the RowMapper callback interface
           */
          return (ValueObj) jdbcTemplate.queryForObject(query,
                  new Object[] { key }, new RowMapper() {
                      public Object mapRow(ResultSet resultSet, int rowNum)
                              throws SQLException {
                          return new ValueObj(resultSet.getString("KEY"),
                                  resultSet.getString("VALUE"));
                      }
                  });
      }
      public ServiceDAOImpl() {
  
      }
  }
  </code></pre>
  
  <p>Now, at the server start up injection is happening fine and when we use the dataSource in serviceDAOImpl the connection is happening fine. But the very first time the database call is made it takes around 3 mins to get the response back. I think this is because the pool creation is done during the first call and we have set the parameter  <code>"initialSize" = 50</code> in <code>applicationConext.xml</code>. </p>
  
  <p>So, to avoid this we need a way in which the pool can be created during the application startup itself and can be used directly. </p>
  
  <p>Please suggest. Let me know if any clarification required.</p>
  
  <p>Regards
  Saroj</p>