Question

It is a Q&A-style.

While using SessionFactoryUtils.getSession to get a session and doing my query, it can be call only limited count and will be waited in calling after that limit count. Why it happens?

    try {
        SessionFactory sessionFactory = getHibernateTemplate().getSessionFactory();
        Session session = SessionFactoryUtils.getSession(sessionFactory, true);
        Query query = session.getNamedQuery("updateAField");

        query.setParameterList("states", states);

        int updatedCount = query.executeUpdate();
        return updatedCount;
    } catch (Exception e) {
        logger.error(e.toString());
        throw new Throwable(e);
    }
Was it helpful?

Solution

Because you are getting session from Hibernate but you do not release it. So by calling SessionFactoryUtils.closeSession(session); you have to release used session in the end of your function. That limit count in calling this function is your database connection pool size. It will be:

    SessionFactory sessionFactory = null;
    Session session = null;
    try {
        sessionFactory = getHibernateTemplate().getSessionFactory();
        session = SessionFactoryUtils.getSession(sessionFactory, true);
        Query query = session.getNamedQuery("updateAField");

        query.setParameterList("states", states);

        int updatedCount = query.executeUpdate();
        return updatedCount;
    } catch (Exception e) {
        logger.error(e.toString());
        throw new Throwable(e);
    } finally {
        if (session != null) {
            SessionFactoryUtils.closeSession(session);
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top