svn commit: r1797074 - /ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/survey/SurveyWrapper.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1797074 - /ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/survey/SurveyWrapper.java

jleroux@apache.org
Author: jleroux
Date: Wed May 31 17:16:15 2017
New Revision: 1797074

URL: http://svn.apache.org/viewvc?rev=1797074&view=rev
Log:
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

Modified:
    ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/survey/SurveyWrapper.java

Modified: ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/survey/SurveyWrapper.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/survey/SurveyWrapper.java?rev=1797074&r1=1797073&r2=1797074&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/survey/SurveyWrapper.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/survey/SurveyWrapper.java Wed May 31 17:16:15 2017
@@ -397,18 +397,14 @@ public class SurveyWrapper {
     public List<GenericValue> getQuestionResponses(GenericValue question, int startIndex, int number) throws SurveyWrapperException {
         List<GenericValue> resp = null;
         boolean beganTransaction = false;
-        try {
+        int maxRows = startIndex + number;
+        try (EntityListIterator eli = this.getEli(question, maxRows)) {
             beganTransaction = TransactionUtil.begin();
-
-            int maxRows = startIndex + number;
-            EntityListIterator eli = this.getEli(question, maxRows);
             if (startIndex > 0 && number > 0) {
                 resp = eli.getPartialList(startIndex, number);
             } else {
                 resp = eli.getCompleteList();
             }
-
-            eli.close();
         } catch (GenericEntityException e) {
             try {
                 // only rollback the transaction if we started one...
@@ -671,10 +667,8 @@ public class SurveyWrapper {
         long total = 0;
 
         boolean beganTransaction = false;
-        try {
+        try (EntityListIterator eli = this.getEli(question, -1)) {
             beganTransaction = TransactionUtil.begin();
-
-            EntityListIterator eli = this.getEli(question, -1);
             if (eli != null) {
                 GenericValue value;
                 while (((value = eli.next()) != null)) {
@@ -690,8 +684,6 @@ public class SurveyWrapper {
                         total++; // increment the count
                     }
                 }
-
-                eli.close();
             }
         } catch (GenericEntityException e) {
             try {