From 21401486e0f5e664fce30fa00c8a786edd8224ac Mon Sep 17 00:00:00 2001 From: Jacques Le Roux Date: Thu, 1 Jun 2017 08:12:12 +0000 Subject: [PATCH] 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@1797159 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/ofbiz/common/FindServices.java | 8 +--- .../ofbiz/common/login/LoginServices.java | 44 +++++++++---------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/framework/common/src/main/java/org/apache/ofbiz/common/FindServices.java b/framework/common/src/main/java/org/apache/ofbiz/common/FindServices.java index 8370d2dcc6..a0e7065c1b 100644 --- a/framework/common/src/main/java/org/apache/ofbiz/common/FindServices.java +++ b/framework/common/src/main/java/org/apache/ofbiz/common/FindServices.java @@ -438,11 +438,9 @@ public class FindServices { int start = viewIndex.intValue() * viewSize.intValue(); List list = null; Integer listSize = 0; - try { - EntityListIterator it = (EntityListIterator) result.get("listIt"); + try (EntityListIterator it = (EntityListIterator) result.get("listIt")) { list = it.getPartialList(start+1, viewSize); // list starts at '1' listSize = it.getResultsSizeAfterPartialList(); - it.close(); } catch (Exception e) { Debug.logInfo("Problem getting partial list" + e,module); } @@ -752,13 +750,11 @@ public class FindServices { List list = null; GenericValue item= null; - try { - EntityListIterator it = (EntityListIterator) result.get("listIt"); + try (EntityListIterator it = (EntityListIterator) result.get("listIt")) { list = it.getPartialList(1, 1); // list starts at '1' if (UtilValidate.isNotEmpty(list)) { item = list.get(0); } - it.close(); } catch (Exception e) { Debug.logInfo("Problem getting list Item" + e,module); } diff --git a/framework/common/src/main/java/org/apache/ofbiz/common/login/LoginServices.java b/framework/common/src/main/java/org/apache/ofbiz/common/login/LoginServices.java index af082d39f1..c8757f1102 100644 --- a/framework/common/src/main/java/org/apache/ofbiz/common/login/LoginServices.java +++ b/framework/common/src/main/java/org/apache/ofbiz/common/login/LoginServices.java @@ -150,7 +150,7 @@ public class LoginServices { // check the user login object again try { - userLogin = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", username).cache(isServiceAuth).queryOne(); + userLogin = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", username).cache(isServiceAuth).queryOne(); } catch (GenericEntityException e) { Debug.logWarning(e, "", module); } @@ -434,29 +434,29 @@ public class LoginServices { // Not saving password history, so return from here. return; } - - EntityListIterator eli = EntityQuery.use(delegator) - .from("UserLoginPasswordHistory") - .where("userLoginId", userLoginId) - .orderBy("-fromDate") - .cursorScrollInsensitive() - .queryIterator(); + EntityQuery eq = EntityQuery.use(delegator) + .from("UserLoginPasswordHistory") + .where("userLoginId", userLoginId) + .orderBy("-fromDate") + .cursorScrollInsensitive(); Timestamp nowTimestamp = UtilDateTime.nowTimestamp(); - GenericValue pwdHist; - if ((pwdHist = eli.next()) != null) { - // updating password so set end date on previous password in history - pwdHist.set("thruDate", nowTimestamp); - pwdHist.store(); - // check if we have hit the limit on number of password changes to be saved. If we did then delete the oldest password from history. - eli.last(); - int rowIndex = eli.currentIndex(); - if (rowIndex==passwordChangeHistoryLimit) { - eli.afterLast(); - pwdHist = eli.previous(); - pwdHist.remove(); + + try (EntityListIterator eli = eq.queryIterator()) { + GenericValue pwdHist; + if ((pwdHist = eli.next()) != null) { + // updating password so set end date on previous password in history + pwdHist.set("thruDate", nowTimestamp); + pwdHist.store(); + // check if we have hit the limit on number of password changes to be saved. If we did then delete the oldest password from history. + eli.last(); + int rowIndex = eli.currentIndex(); + if (rowIndex==passwordChangeHistoryLimit) { + eli.afterLast(); + pwdHist = eli.previous(); + pwdHist.remove(); + } } } - eli.close(); // save this password in history GenericValue userLoginPwdHistToCreate = delegator.makeValue("UserLoginPasswordHistory", UtilMisc.toMap("userLoginId", userLoginId,"fromDate", nowTimestamp)); @@ -904,7 +904,7 @@ public class LoginServices { public static void checkNewPassword(GenericValue userLogin, String currentPassword, String newPassword, String newPasswordVerify, String passwordHint, List errorMessageList, boolean ignoreCurrentPassword, Locale locale) { Delegator delegator = userLogin.getDelegator(); - boolean useEncryption = "true".equals(EntityUtilProperties.getPropertyValue("security", "password.encrypt", delegator)); + boolean useEncryption = "true".equals(EntityUtilProperties.getPropertyValue("security", "password.encrypt", delegator)); String errMsg = null;