Reputation: 828
I'm making a RESTful service that communicates with a database, using Hibernate as an ORM.
The problem I am facing is Hibernate's connection pool limit that throw the exception whenever I reach the limit.
Exception in thread "main" org.hibernate.HibernateException: The internal connection pool has reached its maximum size and no connection is currently available!
1) I have tried to set the maximum pool size in the hibernate.cfg.xml
<property name="connection.pool_size">10</property>
2) I have tried instead of opening a new Session every time, getting the current connection
public static Session getCurrentSession(){
try{
return sessionFactory.getCurrentSession();
}
catch(Exception e){
try {
return sessionFactory.openSession();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
I always reach the limit
eventually.
Is there a way to totally overcome that ?
Upvotes: 1
Views: 4239
Reputation: 2371
I didn't found anyway to set no limit on hibernate connection pool. However, from this answer: Hibernate config connection pool size
You should not use hibernate's pooling mecanism, because it is not suitable for production (as you can see...). You might want to use a pooling API like c3p0 or hikariCP (I heard that DBCP was quite old).
There is a "c3p0.minPoolSize" parameter for c3p0, but no mandatory max size, so it will grow as you need. And it is easy to integrate with hibernate (http://www.mchange.com/projects/c3p0/ and https://www.mkyong.com/hibernate/how-to-configure-the-c3p0-connection-pool-in-hibernate/ if you're using spring and maven)
However, with your current configuration, if there is no maximum limit on how much max connections you have in your app before it crashes, there might be a leak (check if you close all your connections).
Upvotes: 1
Reputation: 31891
2) I have tried instead of opening a new Session every time, ...
I'd assume that in your usual code, you're opening your session like this:
Session session = sessionFactory.openSession();
The Exception
that you've reported usually occurs when you aren't closing your session. However, even if it seems that you've closed your session
but there are possibilities that some exception is occurring that isn't allowing the control to reach session.close()
statement.
Session session = sessionFactory.openSession();
statement1;
statement2; // <-- Exception occurs here
statement3;
session.close();// <-- because of above Exception, your control never reaches here.
Hence the best practice in such a scenario is to write your session.close()
in a finally block like this.
Session session;
try {
session = sessionFactory.openSession();
statement1;
statement2; // <-- Exception occurs here
statement3;
}
finally {
session.close();// <-- although there's an exception above, your control won't leave without executing this statement.
}
If you're using Java 7 and above, then you can also use try with resources
. oracle doc
try (Session session = sessionFactory.openSession()) {
statement1;
statement2;
statement3;
}
Upvotes: 2