0

I ran this query but after a while, I got Closed Resultset error. I'm sure the query is running in the database (I have checked the oracle side).

public static void migrateMessagesToElastic(Long firstMsgId, Long lastMsgId, Integer size) {
    int offset = 0;
    size = size != null ? size : 100;
    List<Long> messageIdList;
    List<Message> messageList = new ArrayList<>();
    do {
        try (HibernateSession session = SessionManager.openSession()) {
            messageIdList = fetchMessageIds(firstMsgId, lastMsgId, offset, size, session);
            messageList = messageIdList.stream().map(e -> MessageCRUD.loadMessage(e, session)).collect(Collectors.toList());
            bulkAddToElastic(messageList, session);
            offset += size;
            if (messageList.size() >= 1) {
                CacheUtil.putLastMigratedMessageId(messageList.get(messageList.size() - 1).getId());
            }
            System.out.println("number of migrated messages : " + offset);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    while (size <= messageList.size());
}

in this method I query to get 50 ids, and I got nothings but an error of closed resultset on getting results of the query.

private static List<Long> fetchMessageIds(Long firstMsgId, Long lastMsgId, int offset, int size, HibernateSession session) throws Exception {
    try {
        String hql = "select m.id from Message m left outer join m.excludedUsers where m.id >= :firstMsgId";
        if (lastMsgId != null) {
            hql += "  and m.id <= :lastMsgId ";
        }
        hql += " order by m.id";
        Query query = session.createQuery(hql)
                .setParameter("firstMsgId", firstMsgId);
        if (lastMsgId != null) {
            query.setParameter("lastMsgId", lastMsgId);
        }
        List<Long> ids = (List<Long>) query
                .setFirstResult(offset)
                .setMaxResults(size)
                .list();
        return ids;
    } catch (Exception ex) {
        ex.printStackTrace();
        throw ex;
    }
}

and This is the error:

enter image description here

lia
  • 193
  • 2
  • 2
  • 12
  • Some thoughts without actually understanding that..... size : 100; and you want to collect 50 , somewhere you are not closing the connection and query before doing another query and connection https://stackoverflow.com/questions/4507440/must-jdbc-resultsets-and-statements-be-closed-separately-although-the-connection – Samuel Marchant Mar 07 '23 at 14:57
  • Post the full stack trace – Christian Beikov Mar 13 '23 at 08:05

0 Answers0