0

We are using Hibernate+Spring for a module and we used @ManyToOne(fetch = FetchType.LAZY) on multiple fields in multiple POJOs. While querying for these POJOs through HQL, I am opening a HibernateSession and returning the result from the DAO method. I wasn't closing these HibernateSesions because i couldn't get the FK objects through getter methods.

Here is one of the simplest methods we implemented :

public List<QmsCapaWfUserDetails> getCapaWfUserDetailsesByWfIdsList(List<Integer> wfIdsList) {
        logger.debug("QmsCapaDao getCapaWfUserDetailsesByWfIdsList() Start");
        List<QmsCapaWfUserDetails> capaWfUserDetailses = null;
        if (!Utilities.IsEmpty(wfIdsList)) {
            StringBuffer hqlQuery = null;
            Query query = null;
            Session hibernateSession = sessionFactory.openSession();
            hqlQuery = new StringBuffer();
            hqlQuery.append("From QmsCapaWfUserDetails capaWfUserDetails where capaWfUserDetails.capaWfDetails.id in (:wfIdsList) ");
            query = hibernateSession.createQuery(hqlQuery.toString());
            if (query != null) {
                query.setParameterList("wfIdsList", wfIdsList);
            }
            capaWfUserDetailses = query.list();
            hibernateSession.flush();
        }
        logger.debug("QmsCapaDao getCapaWfUserDetailsesByWfIdsList() End");
        return capaWfUserDetailses;
    }

I thought flushing these sessions could do something. But it appears not.

I know that because of these too many open sessions, our application has frozen. If i close these sessions, i can't access the lazily loaded FKs.

What could be the best solution..!?

Shaik Bajivali
  • 167
  • 1
  • 16
  • Use proper transactional boundaries and let spring manage them and the session (use `getCurrentSession` instead of `openSession`). – M. Deinum Jul 06 '22 at 09:40
  • @Deinum, if we use "getCurrentSession()" too many times on a busy server, doesn't it affect the performance of the application entirely..? Because i tried it and it didn't made any difference on performance. – Shaik Bajivali Jul 06 '22 at 10:47
  • Read... You should use the combination of Spring managing the transaction and thus the session. If you only use `getCurrentSession` without spring managing things for you you are only hiding the `openSession`. Don't take the only part that suits you you will need both. However you asked for the best solution which is a dedicated query which gets everything in one go instead of using lazy loading (which is basically leading you to an 1+N select problem). – M. Deinum Jul 06 '22 at 11:24

1 Answers1

0

As already mentioned by Deinum, you should use sessionFactory.getCurrentSession() instead of sessionFactory.openSession().

For a detailed comparison between this methods, read here.

If you really don't want to let your sessions managed by Hibernate directly and want to close it yourself have a look here.

Nico
  • 111
  • 10
  • Hi, I don't want to clsoe the sessionFactory as per the second link that u have shared. And i think using "sessionFactory.getCurrentSession()" causes the same performance issue as there will be too many places it will be used and the server is quite busy. – Shaik Bajivali Jul 06 '22 at 10:50
  • Then you have to use `getCurrentsession()`. If you want to use `sessionFactory.openSession()` you HAVE to close the sessions yourself. Otherwise they will not be closed @ShaikBajivali – Nico Jul 06 '22 at 11:01
  • Yes.. I'm closing the sessions and Trying a workaround as mentined here : (https://stackoverflow.com/a/39960438/8772301) – Shaik Bajivali Jul 06 '22 at 11:04
  • I think you should rework your question in this case. It's not clear that you had a problem with your entities. The question itself is misleading so I assumed that you wanted to know about sessionhandling... – Nico Jul 06 '22 at 11:10