title: Spring 3 MVC Hibernate 3.5.4 hibernateTemplate not closing connections (non-transactional)
body: <p>We are using Spring MVC 3.0.5.RELEASE with Hibernate 3.5.4-Final without transactions. Every time we access the database through the hibernateTemplate it creates a new connection and seemingly never closes them.</p>
  
  <p>UPDATE: we've set maxActive and maxIdle to 5. The application will hang when it tries to open the 6th connection. We allow 100 mysql connections.</p>
  
  <p>Our hibernateTemplate is Autowired, so we're not directly managing these connections. Any ideas on how to make sure these connections are closed?</p>
  
  <p>Here's our spring configuration for hibernate:</p>
  
  <pre><code>&lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
      &lt;property name="driverClassName"&gt;
          &lt;value&gt;${jdbc.driverClassName}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="url"&gt;
          &lt;value&gt;${jdbc.url}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="username"&gt;
          &lt;value&gt;${jdbc.username}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="password"&gt;
          &lt;value&gt;${jdbc.password}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="maxActive"&gt;
          &lt;value&gt;5&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="maxIdle"&gt;
          &lt;value&gt;5&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="removeAbandoned"&gt;
          &lt;value&gt;true&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="removeAbandonedTimeout"&gt;
          &lt;value&gt;30&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;alias name="dataSource" alias="userInfoDataSource"/&gt;
  
  &lt;!-- Hibernate SessionFactory --&gt;
  &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
      &lt;property name="dataSource"&gt;
          &lt;ref local="dataSource"/&gt;
      &lt;/property&gt;
      &lt;property name="packagesToScan" value="com.domain"/&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;prop key="hibernate.connection.release_mode"&gt;after_statement&lt;/prop&gt;
              &lt;prop key="hibernate.transaction.flush_before_completion"&gt;true&lt;/prop&gt;                
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Here is our HibernateRepository implementation:</p>
  
  <pre><code>package com.dataAccess.impl;
  
  import org.hibernate.criterion.DetachedCriteria;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.orm.hibernate3.HibernateTemplate;
  import org.springframework.stereotype.Repository;
  
  import java.util.List;
  
  @Repository
  public abstract class HibernateRepositoryImpl&lt;T&gt; implements com.dataAccess.Repository&lt;T&gt; {
  
    @Autowired
    protected HibernateTemplate hibernateTemplate;
  
    public List&lt;T&gt; find(String query) {
      return hibernateTemplate.find(query);
    }
  
    @Override
    public void saveOrUpdate(T ENTITY) {
      hibernateTemplate.saveOrUpdate(ENTITY);
    }
  
    @Override
    public List&lt;T&gt; find(DetachedCriteria criteria) {
      return hibernateTemplate.findByCriteria(criteria);
    }
  
    @Override
    public void delete(T ENTITY) {
      hibernateTemplate.delete(ENTITY);
    }
  }
  </code></pre>
  
  <p>Here is an example of our we are using it, which seems to leak connections:</p>
  
  <pre><code>package com.dataAccess.impl;
  
  import com.domain.Trigger;
  import org.hibernate.criterion.DetachedCriteria;
  import org.hibernate.criterion.Restrictions;
  import org.springframework.stereotype.Repository;
  
  import java.util.List;
  
  @Repository
  public class TriggerRepository extends HibernateRepositoryImpl&lt;Trigger&gt; {
    public List&lt;Trigger&gt; getActiveTriggers(Integer patientId) {
      DetachedCriteria findActiveTriggers = DetachedCriteria.forClass(Trigger.class).add(
          Restrictions.and(
              Restrictions.eq("patientId", patientId),
              Restrictions.eq("active", true)
          )
      );
      return super.find(findActiveTriggers);
    }
    public List&lt;Trigger&gt; getInActiveTriggers(Integer patientId) {
      DetachedCriteria findActiveTriggers = DetachedCriteria.forClass(Trigger.class).add(
          Restrictions.and(
              Restrictions.eq("patientId", patientId),
              Restrictions.eq("active", false)
          )
      );
      return super.find(findActiveTriggers);
    }
  
    public Trigger get(Integer triggerId) {
      return hibernateTemplate.get(Trigger.class, triggerId);
    }
  }
  </code></pre>