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


Alessandro

I'm using Hibernate for the first time in a university project and I'm still new to it. I think I followed all the instructions given by my professor and some tutorials I read, but I keep getting the Exception 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!

All I want to do is store an object ( AbitazioneDB ) into an already created MySql database. Here is my config file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- 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" />

    </session-factory>
</hibernate-configuration>

Here is my AbitazioneDB class (I will omit the getter and setter methods):

@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;
    }
}

Here 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;
    }
}

Here is my TestHibernate class with the following mainmethods:

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!");

    }
}

When I run TestHibernate I always get that exception and I don't know why. Maybe I should change the connection.pool_sizeproperties, but if I do I just seem to get more errors. Can someone help me?

Pritam Banerjee

Try this:

HibernateUtil:

public static Session getHibernateSession() {

    final SessionFactory sf = new Configuration()
        .configure("yourConfig.cfg.xml").buildSessionFactory();

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

Instead of using SessionFactory, use Session:

final Session session = HibernateUtil.getHibernateSession();

Then use:

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

Hope this helps.

If you don't need it, you won't actually have that connection pool property in your hbm.

Related


OrientDB - maximum pool size reached for a given partition

Omega Silva team, We have a single page web application that uses a set of REST APIs. The REST API layer is based on Jersey and it only calls the OrientDB REST API. We call OrientDB functions through the OrientDB REST API. We use OrientDB 2.0.15 OrientDB keeps

The trash can has reached its maximum size?

adobe I'm using Kubuntu 11.04 and when removing in Krusader I see "Thrash has reached max size" even if there is 10M in it ~/.local/share/Trash/files. So my question is why? How do I set the allowed size of the trash can? adobe This UserAction solves it: mv %a

.NET SQL connection exceeds maximum pool size

Han Ji Ho I wrote a simple app to test max pool size = 50 in connection string. static void Test1() { string connectionString = @"Server=.;Database=appsdb;Trusted_Connection=True;Application Name=JH;Max Pool Size=50"; for (int i = 1; i

OrientDB - maximum pool size reached for a given partition

Omega Silva team, We have a single page web application that uses a set of REST APIs. The REST API layer is based on Jersey and it only calls the OrientDB REST API. We call OrientDB functions through the OrientDB REST API. We use OrientDB 2.0.15 OrientDB keeps

No buffer space available, maximum number of connections reached

Jiang package Simple; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.sql.Time; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.ArrayList; import jav

The trash can has reached its maximum size?

adobe I'm using Kubuntu 11.04 and when removing in Krusader I see "Thrash has reached max size" even if there is 10M in it ~/.local/share/Trash/files. So my question is why? How do I set the allowed size of the trash can? adobe This UserAction solves it: mv %a

OrientDB - maximum pool size reached for a given partition

Omega Silva team, We have a single page web application that uses a set of REST APIs. The REST API layer is based on Jersey and it only calls the OrientDB REST API. We call OrientDB functions through the OrientDB REST API. We use OrientDB 2.0.15 OrientDB keeps

OrientDB - maximum pool size reached for a given partition

Omega Silva team, We have a single page web application that uses a set of REST APIs. The REST API layer is based on Jersey and it only calls the OrientDB REST API. We call OrientDB functions through the OrientDB REST API. We use OrientDB 2.0.15 OrientDB keeps

.NET SQL connection exceeds maximum pool size

Han Ji Ho I wrote a simple app to test max pool size = 50 in connection string. static void Test1() { string connectionString = @"Server=.;Database=appsdb;Trusted_Connection=True;Application Name=JH;Max Pool Size=50"; for (int i = 1; i

No buffer space available, maximum number of connections reached

Jiang package Simple; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.sql.Time; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.ArrayList; import jav

The trash can has reached its maximum size?

adobe I'm using Kubuntu 11.04 and when removing in Krusader I see "Thrash has reached max size" even if there is 10M in it ~/.local/share/Trash/files. So my question is why? How do I set the allowed size of the trash can? adobe This UserAction solves it: mv %a