From 4c8bf0dec2f279cb5a5014098c3b3526b53ff521 Mon Sep 17 00:00:00 2001 From: Philipp Schneider Date: Thu, 6 Dec 2012 22:14:47 +0100 Subject: [PATCH] =?UTF-8?q?DATAMONGO-583=20-=20Using=20while=20(=E2=80=A6)?= =?UTF-8?q?=20instead=20of=20for=20(=E2=80=A6)=20for=20DBCursors=20to=20av?= =?UTF-8?q?oid=20memory=20leak.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of iterating over the DBCursor using a for-loop we now use a while-loop to avoid the potential memory leak outlined in [0]. [0] https://jira.mongodb.org/browse/JAVA-664 --- .../data/mongodb/core/MongoTemplate.java | 59 +++++++++++++------ 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java index 2dd85c7f4..8f7425ce5 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java @@ -1513,19 +1513,32 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { CursorPreparer preparer, DbObjectCallback objectCallback, String collectionName) { try { - DBCursor cursor = collectionCallback.doInCollection(getAndPrepareCollection(getDb(), collectionName)); - if (preparer != null) { - cursor = preparer.prepare(cursor); + DBCursor cursor = null; + + try { + + cursor = collectionCallback.doInCollection(getAndPrepareCollection(getDb(), collectionName)); + + if (preparer != null) { + cursor = preparer.prepare(cursor); + } + + List result = new ArrayList(); + + while (cursor.hasNext()) { + DBObject object = cursor.next(); + result.add(objectCallback.doWith(object)); + } + + return result; + + } finally { + + if (cursor != null) { + cursor.close(); + } } - - List result = new ArrayList(); - - for (DBObject object : cursor) { - result.add(objectCallback.doWith(object)); - } - - return result; } catch (RuntimeException e) { throw potentiallyConvertRuntimeException(e); } @@ -1535,15 +1548,27 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { DocumentCallbackHandler callbackHandler, String collectionName) { try { - DBCursor cursor = collectionCallback.doInCollection(getAndPrepareCollection(getDb(), collectionName)); - if (preparer != null) { - cursor = preparer.prepare(cursor); + DBCursor cursor = null; + + try { + cursor = collectionCallback.doInCollection(getAndPrepareCollection(getDb(), collectionName)); + + if (preparer != null) { + cursor = preparer.prepare(cursor); + } + + while (cursor.hasNext()) { + DBObject dbobject = cursor.next(); + callbackHandler.processDocument(dbobject); + } + + } finally { + if (cursor != null) { + cursor.close(); + } } - for (DBObject dbobject : cursor) { - callbackHandler.processDocument(dbobject); - } } catch (RuntimeException e) { throw potentiallyConvertRuntimeException(e); }