title: Spring JdbcTemplate - Connections - Unable to Close it seems
body: <p>I have a configured data source (mySQL) inside my Spring Application as below :</p>
  
  <pre><code>&lt;bean id="dataSource"
          class="org.apache.commons.dbcp.BasicDataSource"&gt;
          &lt;property name="driverClassName"&gt;
              &lt;value&gt;${db.driver}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="url"&gt;
              &lt;value&gt;${db.url}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="username"&gt;
              &lt;value&gt;${db.username}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="password"&gt;
              &lt;value&gt;${db.password}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="defaultAutoCommit"&gt;
          &lt;value&gt;${db.con.defaultAutoCommit}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="initialSize" value="12"/&gt;
          &lt;property name="maxActive"  value="10"/&gt;
          &lt;property name="maxIdle" value="2"/&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p>Inside my code I fetch jdbcTemplate and executing few insert statements as below :`</p>
  
  <pre><code>&lt;bean id="doTransaction" class="com.lch.spring.BusinessComponents.DoTransaction"&gt;
          &lt;property name="jdbcTemplate"&gt;&lt;ref bean="jdbcTemplate"/&gt;&lt;/property&gt;
          &lt;property name="namedParameterJdbcTemplate"&gt;&lt;ref bean="namedParameterJdbcTemplate"/&gt;&lt;/property&gt;
      &lt;/bean&gt;`
  </code></pre>
  
  <p>and the code that do execute an insert statement is :</p>
  
  <pre><code>public int insertLCH_BUSINESS(LCH_BUSINESS lchBusiness) {
      int businessId = -1;
      try {
          log.info("Start Doing Transaction - Business");
          KeyHolder keyHolder = new GeneratedKeyHolder();
          SqlParameterSource parametersSource = new BeanPropertySqlParameterSource(
                  lchBusiness);
          getNamedParameterJdbcTemplate().update(
                  SQLQueries.INSERTLCH_BUSINESS, parametersSource, keyHolder);
          businessId = keyHolder.getKey().intValue();
      } catch (Exception e) {
          e.printStackTrace();
      }
      return businessId;
  }
  </code></pre>
  
  <p>Problem: when I executed this insertion code for several times I am getting pool exhausted error.</p>
  
  <pre><code>     org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool exhausted
   ...............
  
          Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool exhausted
              at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:103)
              at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
              at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
              at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
              ... 38 more
          Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
              at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:756)
              at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:95)
              ... 41 more
  </code></pre>
  
  <p>I can see in the logs as Jdbc Connection returned to DataSource by Spring as below :</p>
  
  <pre><code>[http-8080-2] DEBUG [JdbcTemplate.java              : 570 ][Dec 17 2012 00:43:09.531] Executing prepared SQL statement [INSERT INTO `addressinfo` (`address1`,`address2`,`city`,`state`,`country`,`zipcode`,`landmark`) VALUES (?,?,?,?,?,?,?)]
  [http-8080-2] DEBUG [DataSourceUtils.java           : 110 ][Dec 17 2012 00:43:09.532] Fetching JDBC Connection from DataSource
  [http-8080-2] DEBUG [JdbcTemplate.java              : 860 ][Dec 17 2012 00:43:09.564] SQL update affected 1 rows and returned 1 keys
  [http-8080-2] DEBUG [DataSourceUtils.java           : 332 ][Dec 17 2012 00:43:09.565] Returning JDBC Connection to DataSource
  </code></pre>
  
  <p><br>
  I tried using C3P0 and Bounre CP with no luck. Do I need to close the connections explicitly? I believe jdbcTemplate will do that as I read in the docs.
  <br>
  Can some one able to help here?</p>
  
  <p>Thanks<br>
  Gopi<br>
  www.allibilli.com</p>