14

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"
    "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>

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

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

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 main method:

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 have no idea why. Maybe I should change the connection.pool_size property, but if I do so it seems that I only get more errors. Can someone help me?

4
  • Don't use session.close() if you've obtained session via getCurrentSession(). That's probably exhausting your connection pool. Commented Aug 15, 2016 at 18:44
  • Thanks for the answer, but it didn't work. Still getting the same exception. Commented Aug 15, 2016 at 18:47
  • That means your connection pool has really exhausted all the connections, are there too many long running database calls ? Commented Aug 15, 2016 at 18:50
  • This is the first and only (at least for now) database call that I'm making. Commented Aug 15, 2016 at 19:01

4 Answers 4

24

Your connection pool has exhausted the connections because you're using single connection in the pool.

<property name="connection.pool_size">1</property>

Change this property to something that you'll be comfortable with, like 100

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks again for the answer, but, as I said in my original post, it didn't seem to help.
Also, as per the official documentation of hibernate: "Hibernate's own connection pooling algorithm is, however, quite rudimentary. It is intended to help you get started and is not intended for use in a production system, or even for performance testing. You should use a third party pool for best performance and stability. Just replace the hibernate.connection.pool_size property with connection pool specific settings. This will turn off Hibernate's internal pool. For example, you might like to use c3p0."
10

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

And instead of using SessionFactory use Session:

final Session session = HibernateUtil.getHibernateSession();

And then use:

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

Hope this helps.

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

9 Comments

Thanks for the answer, but still no results.
@Alessandro Did you try and remove that property from you config xml?
@Alessandro And instead of having that save method inside the POJO class, can you try and have it in the main method.
Yes, now it gives me a number of exceptions, starting with: ERROR: could not read a hi value com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'allarmidomesticidb.hibernate_sequence' doesn't exist And then: Exception in thread "main" org.hibernate.exception.SQLGrammarException: error performing isolated work Should I manually create some tables first?
My last comment is a reply after removing the property from the xml file. Now I'll try to move the "save" method.
|
4
<property name="connection.pool_size">1</property>

Try to change the connection pool size to a bigger size, like 100 or so.

<property name="connection.pool_size">100</property>

1 Comment

This seems like a repeat of this existing answer.
0

I also happily solved this problem by increasing the value of the connection.pool_size property to 100

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.