title: DBCP2 initialization
body: <p>I'm using DBCP2 with Spring Security (XML configuration). My problem is that everytime that I made a INSERT or UPDATE creates connections (initialSize = 100), so after a time I get "Too many connections". </p>
  
  <p>I do not know if I doing something wrong in my configuration or in another step. </p>
  
  <p>This is my configuration:</p>
  
  <p><strong>1. springSecurity.xml file</strong></p>
  
  <pre><code>&lt;beans:bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"&gt; 
         &lt;beans:property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
         &lt;beans:property name="url" value="${bd.url}" /&gt;
         &lt;beans:property name="username" value="${bd.user}" /&gt;
         &lt;beans:property name="password" value="${bd.password}" /&gt;
         &lt;beans:property name="validationQuery" value="SELECT 1" /&gt;
         &lt;beans:property name="removeAbandonedTimeout" value="30" /&gt;
         &lt;beans:property name="removeAbandonedOnBorrow" value="true" /&gt;
         &lt;beans:property name="removeAbandonedOnMaintenance" value="true" /&gt;
         &lt;beans:property name="initialSize" value="100" /&gt;
         &lt;beans:property name="maxTotal" value="1000" /&gt;
         &lt;beans:property name="maxIdle" value="50" /&gt;
         &lt;beans:property name="timeBetweenEvictionRunsMillis" value="30" /&gt;        
     &lt;/beans:bean&gt;
  
  &lt;beans:bean id="npjt" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate" &gt;
       &lt;beans:constructor-arg ref="dataSource"/&gt;
  &lt;/beans:bean&gt;
  
  &lt;beans:bean id="DAOEmail" class="general.Email"  &gt;  
         &lt;beans:property name="npjt" ref="npjt" /&gt;
  &lt;/beans:bean&gt; 
  
  &lt;beans:bean id="DAOAddress" class="general.Address" &gt;  
      &lt;beans:property name="npjt" ref="npjt" /&gt;
  &lt;/beans:bean&gt; 
  
  &lt;beans:bean id="DAOPhone" class="general.Phone" &gt;  
      &lt;beans:property name="npjt" ref="npjt" /&gt;
  &lt;/beans:bean&gt; 
  
  &lt;beans:bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
      &lt;beans:property name="dataSource" ref="dataSource"/&gt;
  &lt;/beans:bean&gt;
  
  &lt;tx:annotation-driven transaction-manager="txManager"/&gt;
  </code></pre>
  
  <p><strong>2. DAO</strong></p>
  
  <pre><code>public interface DAOEmail{
      public void save(HashMap&lt;String, String&gt; params);
      public void update(HashMap&lt;String, String&gt; params);
  }
  
  public interface DAOPhone{
      public void save(HashMap&lt;String, String&gt; params);
      public void update(HashMap&lt;String, String&gt; params);
  }
  
  public interface DAOAddress{
      public void save(HashMap&lt;String, String&gt; params);
      public void update(HashMap&lt;String, String&gt; params);
  }
  </code></pre>
  
  <p><strong>3. Calling from a Servlet</strong></p>
  
  <pre><code> ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("springSecurity.xml");
   DAOAddress dao = (DAOAddress) context.getBean("DAOAddress");
   dao.save(params);
   context.registerShutdownHook();
  </code></pre>
  
  <p>Same process per Servlet (Phone, email, address)</p>
  
  <p><strong>4. Operations per file (Address, phone, email)</strong></p>
  
  <pre><code>public class Address implements DAOAddress {
  
      private NamedParameterJdbcTemplate npjt;
  
      public NamedParameterJdbcTemplate getNpjt() {
          return npjt;
      }
  
      public void setNpjt(NamedParameterJdbcTemplate npjt) {
          this.npjt = npjt;
      }
  
      @Transactional
      @Override
      public void save(HashMap&lt;String, String&gt; params) {
         try { 
           ....
              npjt.update();  
           ....
         } catch (Exception e) {
              TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
         }
      }
  
  
  @Transactional
      @Override
      public void update(HashMap&lt;String, String&gt; params) {
        try { 
           ....
              npjt.update();  
           ....
         } catch (Exception e) {
              TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
         }
  
      }
  </code></pre>
  
  <p>Thanks!!!</p>