Alessandro Alessandro - 1 year ago 476
Java Question

Java/Hibernate - Exception: The internal connection pool has reached its maximum size and no connection is currently available

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:

Exception in thread "main" org.hibernate.HibernateException: The internal connection pool has reached its maximum size and no connection is currently available!

What I'm trying to do is simply storing an object (AbitazioneDB) into a MySql Database that I have already created. This is my configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

<!-- Connection to the database -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/AllarmiDomesticiDB</property>

<!-- Credentials -->
<property name="hibernate.connection.username">root</property>
<property name="connection.password">password</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<property name="show_sql">true</property>

<!-- Entity -->

<mapping class="it.allarmiDomestici.centraleOperativaRemota.dbWrapper.AbitazioneDB" />


This is my AbitazioneDB class (I will omitt getters and setters):

public class AbitazioneDB {

private Integer id;

private String indirizzo;

private String nomeProprietario;

private String telProprietario;

public AbitazioneDB(){

public AbitazioneDB save(){

SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.getCurrentSession();

Integer id = (Integer);


return this;

This is my HibernateUtil class:

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;

And this is my TestHibernate class, with the

public class TestHibernate {

public static void main(String[] args) {
AbitazioneDB ab = new AbitazioneDB();

ab.setIndirizzo("Via Napoli, 29");
ab.setNomeProprietario("Mario Rossi");



When I run TestHibernate I always get that exception and I have no idea why. Maybe I should change the
property, but if I do so it seems that I only get more errors. Can someone help me?

Answer Source

Try this :


public static Session getHibernateSession() {

    final SessionFactory sf = new Configuration()

    // factory = new Configuration().configure().buildSessionFactory();
    final Session session = sf.openSession();
    return session;

And instead of using SessionFactory use Session:

final Session session = HibernateUtil.getHibernateSession();

And then use:

 Transaction tx = session.beginTransaction();
 // all your methods

Hope this helps.

And you can actually not have that connection pool property in your hbm if you don't need it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download