我在尝试使用mybatis读取大型结果集时遇到oom错误。mybatis版本3.5我想在游标上迭代,而不是加载整个查询结果。有没有人有一个从mybatis select返回光标而不是返回整个结果的工作示例。
[java] SEVERE:Memory usage is low, parachute is non existent, your system may start failing.
[java] java.lang.OutOfMemoryError: Java heap space
[java] AsyncLogger error handling event seq=0, value='null':
[java] java.lang.OutOfMemoryError: Java heap space
[java] 06 Sep 2020 05:34:34,682 [ERROR] (http-nio-0.0.0.0-8243-ClientPoller-1) org.apache.tomcat.util.net.NioEndpoint:
[java] java.lang.OutOfMemoryError: Java heap space
[java] at java.util.Collections$UnmodifiableCollection.iterator(Collections.java:1043) ~[?:?]
[java] at org.apache.tomcat.util.net.NioEndpoint$Poller.timeout(NioEndpoint.java:1471) ~[Bobcat-2.2.jar:?]
[java] at org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:1265) ~[Bobcat-2.2.jar:?]
[java] at java.lang.Thread.run(Thread.java:834) [?:?]
readerdao.xml文件
<select id="downloadelements" resultMap="elementMap">
select *
from sample_table
where element_timestamp between #{startTime} and #{endTime}
and status=#{status}
</select>
readerdao.java文件
public interface ReaderDao {
Cursor<Element> downloadElements(final @NotNull @Param("startTime") ZonedDateTime startTime,
final @NotNull @Param("endTime") ZonedDateTime endTime,
final @NotNull @Param("status") String status);
}
数据库:aurora postgresql
Cursor<Element> elementList = downloadElements(start, end, status);
Iterator<Element> elementIterator = elementList.iterator();
while (elementIterator.hasNext()) {
Element element = ElementIterator.next();
log.info(element.toString());
}
<resultMap id="elementMap" type="Element">
<constructor>
<arg javaType="String" column="status"/>
<arg javaType="java.time.ZonedDateTime" column="start_time"/>
<arg javaType="java.util.Optional" jdbcType="TIMESTAMP" column="end_time"/>
</constructor>
</resultMap>
1条答案
按热度按时间3lxsmp7m1#
因为postgresql驱动程序不支持流式结果(正如@ave所指出的),所以您很可能希望实现结果分页。这通常是通过使用
ORDER BY element_timestamp LIMIT <n> OFFSET <k>
或者使用键集分页。这两种方法中的任何一种都会将较小的批结果加载到内存中,希望通过允许gc分别释放每个批来避免oome。但是应该注意的是,有时增加可用堆内存是最简单的解决方案。ram通常比程序员的时间要便宜,所以也许您应该从估计您要处理多少条记录以及需要多少内存开始。