title: setup spring bean configuration for dbcp2 gives "PoolableConnectionFactory not linked to pool. Calling setPool()"
body: <p>I need to convert dbcp2 java setup code into spring beans</p>
  
  <p>The below code works as expected:</p>
  
  <pre><code>protected  void setupDriver(String connectURI, String username, String password) throws ClassNotFoundException, SQLException{
  
          //
          // First, we'll create a ConnectionFactory that the
          // pool will use to create Connections.
          // We'll use the DriverManagerConnectionFactory,
          // using the connect string passed in the command line
          // arguments.
          //
          ConnectionFactory connectionFactory =
              new DriverManagerConnectionFactory(connectURI, username, password);
  
          //
          // Next we'll create the PoolableConnectionFactory, which wraps
          // the "real" Connections created by the ConnectionFactory with
          // the classes that implement the pooling functionality.
          //
          poolableConnectionFactory = 
                  new PoolableConnectionFactory(connectionFactory, null);
          logger.info("poolableConnectionFactory created");
  
          //
          // Now we'll need a ObjectPool that serves as the
          // actual pool of connections.
          //
          // We'll use a GenericObjectPool instance, although
          // any ObjectPool implementation will suffice.
          //        
          connectionPool =
                  new GenericObjectPool&lt;PoolableConnection&gt;(poolableConnectionFactory,getPoolConfig());
          logger.info("connectionPool created");
  
          // Set the factory's pool property to the owning pool
          poolableConnectionFactory.setPool(connectionPool);
          logger.info("connectionPool is set to poolableConnectionFactory");
  
          //
          // Finally, we create the PoolingDriver itself...
          //
          Class.forName("org.apache.commons.dbcp2.PoolingDriver");
          driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
          logger.info("dbcp2 driver is created");
  
          //
          // ...and register our pool with it.
          //
          driver.registerPool(poolName,connectionPool);
          logger.info("driver is registered");
  
          //
          // Now, we create the PoolingDriver itself,
          // passing in the object pool we created.
          //
          dataSource = new PoolingDataSource&lt;PoolableConnection&gt;(connectionPool);       
          logger.info("dataSource is created");
  
          //
          //Finally we create the JdbcTemplate for sql 
          //operations in DbDAO class
          //
          jdbcTemplate = new JdbcTemplate(dataSource);        
          logger.info("jdbcTemplate is setup");
  
          logger.info("Finally dbcp2 driver setup is completed!");
  
      }   
  
      //Pool initial setup values
      private GenericObjectPoolConfig getPoolConfig(){
  
              logger.info("Let's create the pool config values");
              GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
              poolConfig.setMaxTotal(Integer.parseInt(config.getMaxtotal())); // set number of max connections i.e 25
              poolConfig.setMaxWaitMillis(Long.parseLong(config.getMaxwaitmillis())); //ie. wait for a minute = 60000
              poolConfig.setMaxIdle(Integer.parseInt(config.getMaxidle())); // set max number of idle connections
              /*poolConfig.setTestOnBorrow(true);
              poolConfig.setTestOnReturn(true);*/
              //poolConfig.setTestWhileIdle(true);
              //poolConfig.setTimeBetweenEvictionRunsMillis(10000L);
              //poolConfig.setNumTestsPerEvictionRun(5);
              //poolConfig.setMinEvictableIdleTimeMillis(5000L);
  
              return poolConfig;
  }
  </code></pre>
  
  <p>And this is the beans.xml</p>
  
  <pre><code>&lt;!-- ============ Trauma Database Connection Pooling Beans Settings ================== --&gt;  
      &lt;bean id="connectionFactory" class="org.apache.commons.dbcp2.DriverManagerConnectionFactory"&gt;
          &lt;constructor-arg index="0"  value="${tir.jdbc.url}" /&gt;
          &lt;constructor-arg index="1"  value="${tir.jdbc.username}" /&gt;
          &lt;constructor-arg index="2"  value="${tir.jdbc.password}" /&gt;
      &lt;/bean&gt;
      &lt;!-- Connection Factory --&gt;
      &lt;bean id="poolableConnectionFactory" class="org.apache.commons.dbcp2.PoolableConnectionFactory"&gt;
          &lt;constructor-arg index="0" ref="connectionFactory"/&gt;
          &lt;constructor-arg index="1" &gt;  &lt;null/&gt; &lt;/constructor-arg&gt;
      &lt;/bean&gt;
      &lt;!-- Pool Configs --&gt;
      &lt;bean id="poolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig"&gt;
          &lt;property name="maxTotal" value="${pool.maxtotal}"/&gt;
          &lt;property name="maxIdle" value="${pool.maxidle}"/&gt;
          &lt;property name="minIdle" value="${pool.minidle}"/&gt;
          &lt;property name="maxWaitMillis" value="${pool.maxwaitmillis}"/&gt;
      &lt;/bean&gt;    
      &lt;!-- Connection Pool --&gt;
      &lt;bean id="connectionPool" class="org.apache.commons.pool2.impl.GenericObjectPool"&gt;
          &lt;constructor-arg index="0" ref="poolableConnectionFactory"/&gt;
          &lt;constructor-arg index="1" ref="poolConfig"/&gt;
      &lt;/bean&gt;       
  
      &lt;!-- Datasource gets connection pool --&gt;
      &lt;bean id="dataSource" class="org.apache.commons.dbcp2.PoolingDataSource"&gt;
          &lt;constructor-arg ref="connectionPool"/&gt;
      &lt;/bean&gt;
      &lt;!-- JdbcTemplate bean gets the datasource --&gt;
      &lt;bean id="jdbcTemplateTIR" class="org.springframework.jdbc.core.JdbcTemplate"&gt;  
          &lt;constructor-arg ref="dataSource" /&gt;       
      &lt;/bean&gt;
      &lt;!-- Finally, we create our Database object bean --&gt;
      &lt;bean id="dbdao" class="edu.uams.dao.impl.DBDAO"&gt;
          &lt;property name="jdbcTemplate" ref="jdbcTemplateTIR" /&gt;
      &lt;/bean&gt;
      &lt;!-- ============= END OF Trauma Database Connection Pooling Settings =========== --&gt;
  </code></pre>
  
  <p>In both situations I can use the jDBCTemplate objects, however it gives the following warning:<br>
  <strong><em>"WARN     0[main] - org.apache.commons.dbcp2.PoolingDataSource.(PoolingDataSource.java:65) PoolableConnectionFactory not linked to pool. Calling setPool() to fix the configuration"</em></strong></p>
  
  <p>The reason is obvious: In my java code, I setup </p>
  
  <pre><code>poolableConnectionFactory.setPool(connectionPool);
  </code></pre>
  
  <p>How can I call setPool method from beans? </p>
  
  <p>And how can I also setup this java code in my beans? The constructor of DriverManagerConnectionFactory does not get any constructor of DriverClassName?</p>
  
  <pre><code>            // Finally, we create the PoolingDriver itself...
              //
              Class.forName("org.apache.commons.dbcp2.PoolingDriver");
              driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
              logger.info("dbcp2 driver is created");
  
              //
              // ...and register our pool with it.
              //
              driver.registerPool(poolName,connectionPool);
  </code></pre>