我运行了这个查询,但过了一会儿,我得到了 Closed Resultset 错误。我确定查询正在数据库中运行(我已经检查了 oracle 端)。
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());
}
在这种方法中,我查询以获取 50 个 ID,除了在获取查询结果时出现关闭结果集错误外,我什么都没有得到。
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;
}
}
这是错误: