0

i'm kinda new on web dev, i'm a trainee on a company since last year and i have the following problem:

I'm making a web app with JSF2.3 and Hibernate 5.4.2.Final and c3p0 5.4.2.Final. The thing is everytime i run and go for the login page, i need to check if there is an admin user already registered - i make a count on employee's table based on employee's code - and if there isn't any administrator, then i get a list of country states and render a form register menu. So, i get the session from the sessionfactory.opensession() in mine HibernateUtil.class, do the count and clear/close the session like the snipet:

public Long retornaLong(String query) throws Exception{
            Session session = new HibernateUtil().getSession();
            try {
                return (Long) session.createQuery(query).getSingleResult();
            }finally {
                session.clear();
                session.close();
            }
        }

then i get the country states list from

@SuppressWarnings("unchecked")
    public List<T> retornaList(String query) throws Exception{
        Session session = new HibernateUtil().getSession();
        try {
            return (List<T>) session.createQuery(query).getResultList();
        }finally {
            session.clear();
            session.close();
        }
    }

but if i keep refreshing the page (@viewscoped), like 15+ times, eventually i'll get too many connection exception, this doesn't happen if i use one session for both queries. I think there's no enough time for the session to close, causing a connection leak. I want to use one session for each query, can someone help me. Thanks a lot.

My hibernate.cfg.xml

<hibernate-configuration>

    <!-- a SessionFactory instance listed as /jndi/name -->
    <session-factory>

        <!-- properties -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/vetsnpets?useTimezone=true&amp;serverTimezone=UTC</property>
        <property name="hibernate.connection.username">vetsNpets</property>
        <property name="hibernate.connection.password">123</property>
        <property name="hiberante.show_sql">false</property>
        <property name="hiberante.format_sql">false</property>
        <property name="hbm2ddl.auto">validate</property>
        <property name="current_session_context_class">thread</property>

        <!-- C3P0 -->
        <property name="hibernate.c3p0.initialPoolSize">3</property>
        <property name="hibernate.c3p0.minPoolSize">3</property>
        <property name="hibernate.c3p0.maxPoolSize">20</property>
        <property name="hibernate.c3p0.maxStatements">100</property>
        <property name="hibernate.c3p0.maxStatementsPerConnection">5</property>
        <property name="hibernate.c3p0.maxIdleTime">2700</property>
        <property name="hibernate.c3p0.maxIdleTimeExcessConnections">600</property>
        <property name="hibernate.c3p0.acquireIncrement">1</property>
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Juanito '
  • 1
  • 1
  • Using a 'HibernateUtil' is very oldschool, and effectively there is nothing jsf related in here. Try creating a good unittest. – Kukeltje Apr 27 '19 at 18:57
  • but how this affect the too many connections? – Juanito ' Apr 27 '19 at 18:58
  • Jsf: it doesn't... your code does. Try not doing hibernate stuff from a jsf bean but in a service class/ejb/... and read https://stackoverflow.com/questions/30639785/jsf-controller-service-and-dao#30641460 – Kukeltje Apr 27 '19 at 19:00
  • sorry this is not from jsf bean, its a class wich opens the session and the DAO, the jsf bean only call these methods that i posted – Juanito ' Apr 27 '19 at 19:03
  • Then it is 100% not jsf related. – Kukeltje Apr 27 '19 at 21:59

0 Answers0