title: Java/Hibernate - Exception: The internal connection pool has reached its maximum size and no connection is currently available
body: <p>I'm using Hibernate for the first time for a University project and I'm a bit of a newbie. I think I followed all the instructions given by my professor and by some tutorials I read, but I keep getting the Exception that is in the title:</p>
  
  <pre><code>Exception in thread "main" org.hibernate.HibernateException: The internal connection pool has reached its maximum size and no connection is currently available!
  </code></pre>
  
  <p>What I'm trying to do is simply storing an object (<b>AbitazioneDB</b>) into a MySql Database that I have already created. This is my configuration file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;!-- Connection to the database --&gt;
          &lt;property name="connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
          &lt;property name="connection.url"&gt;jdbc:mysql://localhost:3306/AllarmiDomesticiDB&lt;/property&gt;
  
          &lt;!-- Credentials --&gt;
          &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt;
          &lt;property name="connection.password"&gt;password&lt;/property&gt;
  
          &lt;!-- JDBC connection pool (use the built-in) --&gt;
          &lt;property name="connection.pool_size"&gt;1&lt;/property&gt;
  
          &lt;!-- SQL dialect --&gt;
          &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
  
          &lt;!-- Enable Hibernate's automatic session context management --&gt;
          &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
  
          &lt;!-- Disable the second-level cache  --&gt;
          &lt;property name="cache.provider_class"&gt;org.hibernate.cache.NoCacheProvider&lt;/property&gt;
  
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
  
          &lt;!-- Entity --&gt;
  
          &lt;mapping class="it.allarmiDomestici.centraleOperativaRemota.dbWrapper.AbitazioneDB" /&gt;
  
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>This is my <b>AbitazioneDB</b> class (I will omitt getters and setters):</p>
  
  <pre><code>@Entity
  @Table(name="abitazioni")
  public class AbitazioneDB {
  
      @Id
      @GeneratedValue
      @Column(name="id")
      private Integer id;
  
      @Column(name="indirizzo")
      private String indirizzo;
  
      @Column(name="nome_proprietario")
      private String nomeProprietario;
  
      @Column(name="tel_proprietario")
      private String telProprietario;
  
      public AbitazioneDB(){
          super();
      }
  
      public AbitazioneDB save(){
  
          SessionFactory sf = HibernateUtil.getSessionFactory();
          Session session = sf.getCurrentSession();
          session.beginTransaction();
  
          Integer id = (Integer) session.save(this);
          this.setId(id);
  
          session.getTransaction().commit();      
          session.close();
  
          return this;
      }
  }
  </code></pre>
  
  <p>This is my <b>HibernateUtil</b> class:</p>
  
  <pre><code>public class HibernateUtil {
  
      private static SessionFactory sessionFactory;
  
      private static SessionFactory createSessionFactory() {
          sessionFactory = new Configuration().configure().buildSessionFactory();
          return sessionFactory;
      }
  
      public static SessionFactory getSessionFactory() {
          if (sessionFactory == null)
              sessionFactory = createSessionFactory();
  
          return sessionFactory;
      }
  }
  </code></pre>
  
  <p>And this is my <b>TestHibernate</b> class, with the <code>main</code> method:</p>
  
  <pre><code>public class TestHibernate {
  
      public static void main(String[] args) {
          AbitazioneDB ab = new AbitazioneDB();
  
          ab.setIndirizzo("Via Napoli, 29");
          ab.setNomeProprietario("Mario Rossi");
          ab.setTelProprietario("3333333333");
  
          ab.save();
  
          System.out.println("Ok!");
  
      }
  }
  </code></pre>
  
  <p>When I run <b>TestHibernate</b> I always get that exception and I have no idea why. Maybe I should change the <code>connection.pool_size</code> property, but if I do so it seems that I only get more errors. Can someone help me?</p>