title: How to avoid too many connections on spring+hibernate?
body: <p>I´ve my hibernate configuration inside my spring-context, but after a little time of use I´m getting the too many connections error. Here is my code:
  Spring context:</p>
  
  <pre><code> &lt;!-- Beans Declaration --&gt;
   &lt;bean id="Usuarios" class="com.proximate.model.Usuarios"/&gt;
  &lt;!-- Service Declaration --&gt;
  &lt;bean id="UsuariosService" class="com.proximate.service.UsuariosService"&gt;
      &lt;property name="usuariosDAO" ref="UsuariosDAO" /&gt;
  &lt;/bean&gt;
  &lt;!-- DAO Declaration --&gt;
  &lt;bean id="UsuariosDAO" class="com.proximate.dao.UsuariosDAO"&gt;
      &lt;property name="sessionFactory" ref="SessionFactory" /&gt;
  &lt;/bean&gt;
   &lt;bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"&gt;
          &lt;property name="driverClass" value="com.mysql.jdbc.Driver" /&gt;   
          &lt;property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mydb" /&gt;
          &lt;property name="user" value="user" /&gt;   
          &lt;property name="password" value="password" /&gt;   
          &lt;property name="maxPoolSize" value="50" /&gt;
          &lt;property name="maxStatements" value="0" /&gt;
          &lt;property name="minPoolSize" value="5" /&gt; 
      &lt;/bean&gt; 
      &lt;!-- Session Factory Declaration --&gt;
      &lt;bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="DataSource" /&gt;
          &lt;property name="annotatedClasses"&gt;
              &lt;list&gt;
                  &lt;value&gt;com.proximate.model.Usuarios&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&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.format_sql"&gt;true&lt;/prop&gt;                         
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- Enable the configuration of transactional behavior based on annotations --&gt;
      &lt;tx:annotation-driven transaction-manager="txManager"/&gt;
  
      &lt;context:annotation-config /&gt;
  
      &lt;!-- Transaction Manager is defined --&gt;
      &lt;bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="SessionFactory"/&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p>and here is how I use my session factory inside my dao:</p>
  
  <pre><code>private SessionFactory sessionFactory;
  private HibernateTemplate hibernateTemplate;
  
  public SessionFactory getSessionFactory() {
      return sessionFactory;
  }
  
  public void setSessionFactory(SessionFactory sessionFactory) {
      this.hibernateTemplate = new HibernateTemplate(sessionFactory);
  }
  
  Query query = hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery("SELECT ID FROM usuarios");
  Integer cantidad = new Integer(((BigInteger) query.uniqueResult()).intValue());
  </code></pre>
  
  <p>Why is my code opening so many connections. I heard that using a HibernateUtil class to load a session after logging might be what I need, but how do you implement it when using spring??
  Thanks in advance!!</p>