title: Spring, Hibernate C3P0 Connection Pool, physical connections not closed
body: <p>We have used Spring, Hibernate and C3P0 connection pooling. But connections are not getting closed even though we set <strong>maxIdleTime=3</strong>, <strong>maxConnectionAge=3</strong>, <strong>maxIdleTimeExcessConnections=1</strong>.</p>
  
  <p>Following is <strong>dispatcher-servlet.xml</strong> code</p>
  
  <pre><code>    &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"&gt;
          &lt;property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/&gt;
          &lt;property name="jdbcUrl" value="${jdbcUrl}"/&gt;
          &lt;property name="user" value="${user}"/&gt;
          &lt;property name="password" value="${password}"/&gt;
          &lt;property name="maxPoolSize" value="10"/&gt;
          &lt;property name="initialPoolSize" value="2"/&gt;
          &lt;property name="acquireIncrement"  value="1"/&gt;          
          &lt;property name="minPoolSize" value="1"/&gt;
          &lt;property name="maxIdleTime" value="3"/&gt;
          &lt;property name="maxConnectionAge" value="3"/&gt;
          &lt;property name="maxIdleTimeExcessConnections" value="1"/&gt;        
      &lt;/bean&gt;
  
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt; 
          &lt;property name="dataSource" ref="dataSource"/&gt;
          &lt;property name="configLocation"&gt;
              &lt;value&gt;classpath:hibernate.cfg.xml&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="ConfigurationClass"&gt;
              &lt;value&gt;org.hibernate.cfg.AnnotationConfiguration&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.show_sql"&gt;false&lt;/prop&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.SQLServerDialect&lt;/prop&gt;
              &lt;/props&gt;  
          &lt;/property&gt;         
      &lt;/bean&gt;         
  
      &lt;tx:annotation-driven/&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;mvc:annotation-driven/&gt;
  </code></pre>
  
  <p>Following is <strong>Service</strong> and <strong>DAO</strong> Implementation Code</p>
  
  <p><strong>Service Implemention Class</strong></p>
  
  <pre><code>@Service
  public class SaveFeedbackServiceImpl implements SaveFeedbackService 
  { 
  
      @Autowired
      private SaveFeedBackDAO saveFeedbackDAO;
  
  
      @Transactional
      public void saveStudentTestFeedback(FeedbackMaster fm,String akey,int testid)
      {
          saveFeedbackDAO.saveStudentTestFeedback(fm, akey, testid);
      }
  }
  </code></pre>
  
  <p>DAO Implementation Class</p>
  
  <pre><code>@Repository
  public class SaveFeedBackImpl implements SaveFeedBackDAO
  {
  
          @Autowired
      private SessionFactory sessionFactory; 
  
      @Override
      public void saveStudentTestFeedback(FeedbackMaster fm,String akey,int testid)
      {
  
  
          Criteria criteria = sessionFactory.getCurrentSession().createCriteria(FeedbackMaster.class);
          criteria.add(Restrictions.eq("accesskey", akey)).add(Restrictions.eq("testid", testid));
  
          FeedbackMaster fdkm = (FeedbackMaster) criteria.uniqueResult();
  
          fdkm.setFbq1(fm.getFbq1());
          fdkm.setFbq2(fm.getFbq2());
          fdkm.setFbq3(fm.getFbq3());
          fdkm.setFbq4(fm.getFbq4());
          fdkm.setFbq5(fm.getFbq5());
          fdkm.setFbq6(fm.getFbq6());
          fdkm.setFbq7(fm.getFbq7());
          fdkm.setFbq8(fm.getFbq8());
          fdkm.setFbq9(fm.getFbq9());
          fdkm.setFbq10(fm.getFbq10());
  
  
          sessionFactory.getCurrentSession().update(fdkm);
  
  
          String hql = " UPDATE StudentTestDetail set testStatus=:testStatus "+
                       " WHERE accesskey = :akey and testid=:testid ";
  
           Query query = sessionFactory.getCurrentSession().createQuery(hql);
           query.setParameter("testStatus",4);
           query.setParameter("akey",akey);
           query.setParameter("testid",testid);        
           query.executeUpdate();
  
  
      }
  
  }    
  </code></pre>
  
  <p>Thanks in Advance.</p>