javasqlhibernatejakarta-eescrollableresults

Will database be hit 2 times by this code?


I am trying to improve performance of an api. I need to know whether the second line that's marked will hit database too ? as I want to minimise that.

     StringBuffer queryBuf = new StringBuffer("some query in SQL");
--------->StringBuffer queryBuf2 = new StringBuffer(" SELECT DISTINCT PP.ID FROM ( " + queryBuf + ") PP ");
        Query query1 = getSession().createSQLQuery(queryBuf2.toString());
        query1.setReadOnly(true);
        ScrollableResults results = query1.scroll();
        if (results.isLast() == false)
            results.last();
        int total = results.getRowNumber() + 1;
        results.close();
        logger.debug(">>>>>>TOTAL COUNT<<<<<< = {}", total);

Solution

  • I need to know whether the second line that's marked will hit database too ?

    You are constructing StringBuffer objects in first three lines , why should it hit the DB ! You can use StringBuilder if synchronization is not required !