本文介绍了使用MySQL流式传输大型结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在开发一个使用大型MySQL表的spring应用程序。加载大表时,我得到一个 OutOfMemoryException ,因为驱动程序试图将整个表加载到应用程序内存中。I'm developing a spring application that uses large MySQL tables. When loading large tables, I get an OutOfMemoryException, since the driver tries to load the entire table into application memory.我尝试使用statement.setFetchSize(Integer.MIN_VALUE);但是我打开的每个ResultSet都挂在 close();在线查找我发现这是因为它在关闭ResultSet之前尝试加载任何未读行,但事实并非如此:but then every ResultSet I open hangs on close(); looking online I found that that happens because it tries loading any unread rows before closing the ResultSet, but that is not the case since I do this:ResultSet existingRecords = getTableData(tablename);try { while (existingRecords.next()) { // ... }} finally { existingRecords.close(); // this line is hanging, and there was no exception in the try clause}挂起也发生在小表(3行)上,如果我不关闭RecordSet(发生在一个方法中),那么 connection.close()挂起。The hangs happen for small tables (3 rows) as well, and if I don't close the RecordSet (which happened in one method) then connection.close() hangs.挂起的堆栈跟踪:推荐答案 请勿关闭 ResultSet 两次。Don't close your ResultSets twice.显然,当关闭 Statement 时,它会尝试关闭相应的 ResultSet ,如你可以从堆栈跟踪中看到这两行:Apparently, when closing a Statement it attempts to close the corresponding ResultSet, as you can see in these two lines from the stack trace: I曾经认为挂起是在 ResultSet.close()但它实际上是在 Statement.close()中调用 ResultSet.close()。由于 ResultSet 已经关闭,它只是挂起。I had thought the hang was in ResultSet.close() but it was actually in Statement.close() which calls ResultSet.close(). Since the ResultSet was already closed, it just hung.我们已经替换了所有 ResultSet.close() with results.getStatement()。close()并删除所有 Statement.close() s,问题现在解决了。We've replaced all ResultSet.close() with results.getStatement().close() and removed all Statement.close()s, and the problem is now solved. 这篇关于使用MySQL流式传输大型结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-18 23:34