DATAMONGO-583 - Using while (…) instead of for (…) for DBCursors to avoid memory leak.
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
This commit is contained in:
committed by
Oliver Gierke
parent
cffc27d83a
commit
4c8bf0dec2
@@ -1513,19 +1513,32 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
|||||||
CursorPreparer preparer, DbObjectCallback<T> objectCallback, String collectionName) {
|
CursorPreparer preparer, DbObjectCallback<T> objectCallback, String collectionName) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
DBCursor cursor = collectionCallback.doInCollection(getAndPrepareCollection(getDb(), collectionName));
|
|
||||||
|
|
||||||
if (preparer != null) {
|
DBCursor cursor = null;
|
||||||
cursor = preparer.prepare(cursor);
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
cursor = collectionCallback.doInCollection(getAndPrepareCollection(getDb(), collectionName));
|
||||||
|
|
||||||
|
if (preparer != null) {
|
||||||
|
cursor = preparer.prepare(cursor);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<T> result = new ArrayList<T>();
|
||||||
|
|
||||||
|
while (cursor.hasNext()) {
|
||||||
|
DBObject object = cursor.next();
|
||||||
|
result.add(objectCallback.doWith(object));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
|
||||||
|
if (cursor != null) {
|
||||||
|
cursor.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List<T> result = new ArrayList<T>();
|
|
||||||
|
|
||||||
for (DBObject object : cursor) {
|
|
||||||
result.add(objectCallback.doWith(object));
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
throw potentiallyConvertRuntimeException(e);
|
throw potentiallyConvertRuntimeException(e);
|
||||||
}
|
}
|
||||||
@@ -1535,15 +1548,27 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
|||||||
DocumentCallbackHandler callbackHandler, String collectionName) {
|
DocumentCallbackHandler callbackHandler, String collectionName) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
DBCursor cursor = collectionCallback.doInCollection(getAndPrepareCollection(getDb(), collectionName));
|
|
||||||
|
|
||||||
if (preparer != null) {
|
DBCursor cursor = null;
|
||||||
cursor = preparer.prepare(cursor);
|
|
||||||
|
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) {
|
} catch (RuntimeException e) {
|
||||||
throw potentiallyConvertRuntimeException(e);
|
throw potentiallyConvertRuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user