LPD
LPD

Reputation: 2883

Simulation of “PSQLException: FATAL: sorry, too many clients already”

I was trying to understand the max open sessions, and sessiion open and close behaviours. I came across this, which am not able to understand completely.

I want to simulate the “PSQLException: FATAL: sorry, too many clients already” in my code. I have set max_connections to 10.

and attempted this code:

Session session = HibernateUtil.getSessionFactory().openSession();
        session = HibernateUtil.getSessionFactory().openSession();
        session = HibernateUtil.getSessionFactory().openSession();
        session = HibernateUtil.getSessionFactory().openSession();
        session = HibernateUtil.getSessionFactory().openSession();
        session = HibernateUtil.getSessionFactory().openSession();
        session = HibernateUtil.getSessionFactory().openSession();
        session = HibernateUtil.getSessionFactory().openSession();
        session = HibernateUtil.getSessionFactory().openSession();
        session = HibernateUtil.getSessionFactory().openSession();
        session = HibernateUtil.getSessionFactory().openSession();
        session = HibernateUtil.getSessionFactory().openSession();
        session = HibernateUtil.getSessionFactory().openSession();

I was expecting the error here on the 11th call, which I don't understand. When I run the sql command SELECT datname, numbackends FROM pg_stat_database where datname = 'XXX'; which I beleive will show the open session count, I see it capped at 10.

Kindly explain the behaviour.

Upvotes: 4

Views: 1443

Answers (1)

grasshopper
grasshopper

Reputation: 4068

There's a number of things that may be happening (favoring #2): 1) You didn't restart the postgresql server properly when changing the max number of connections. So, restart the server or see here 2) Hibernate may be using JDBC's pooling system for sessions and you are setting the same session variable repeatedly. Maybe the previous open sessions are garbage collected, and the pool can always make room for the newest session. I would try to make an array of connection object, and also see what happens when you try to do some work with the open sessions.

Upvotes: 1

Related Questions