Improved: EntityListIterator closed but not in case of exception

(OFBIZ-9385)

This is an improvement only because no cases were reported. But obviously in 
case of unlucky exception after the EntityListIterator creation and before it's 
closed the EntityListIterator remains in memory. 
It should be closed in EntityListIterator.finalize() but the less happens there 
the better.

The solution is to use try-with-ressources

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1797160 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jacques Le Roux
2017-06-01 08:14:41 +00:00
parent 21401486e0
commit 12ecb8926f
@@ -452,9 +452,9 @@ public class EntityQuery {
if (dynamicViewEntity == null) {
result = delegator.findList(entityName, makeWhereCondition(useCache), fieldsToSelect, orderBy, findOptions, useCache);
} else {
EntityListIterator it = queryIterator();
result = it.getCompleteList();
it.close();
try (EntityListIterator it = queryIterator()) {
result = it.getCompleteList();
}
}
if (filterByDate && useCache) {
return EntityUtil.filterByCondition(result, this.makeDateCondition());
@@ -510,9 +510,7 @@ public class EntityQuery {
}
public <T> List<T> getFieldList(final String fieldName) throws GenericEntityException {select(fieldName);
EntityListIterator genericValueEli = null;
try {
genericValueEli = queryIterator();
try (EntityListIterator genericValueEli = queryIterator()) {
if (this.distinct) {
Set<T> distinctSet = new HashSet<T>();
GenericValue value = null;
@@ -536,11 +534,6 @@ public class EntityQuery {
return fieldList;
}
}
finally {
if (genericValueEli != null) {
genericValueEli.close();
}
}
}
/**
@@ -551,16 +544,9 @@ public class EntityQuery {
* @see EntityUtil#getPagedList
*/
public PagedList<GenericValue> queryPagedList(final int viewIndex, final int viewSize) throws GenericEntityException {
EntityListIterator genericValueEli = null;
try {
genericValueEli = queryIterator();
try (EntityListIterator genericValueEli = queryIterator()) {
return EntityUtil.getPagedList(genericValueEli, viewIndex, viewSize);
}
finally {
if (genericValueEli != null) {
genericValueEli.close();
}
}
}
}