This is an automated email from the ASF dual-hosted git repository.

paulfoxworthy pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new c04d699dfc Fixed: Prevent resource leaks in EntitySQLProcessor.groovy 
(OFBIZ-12836)
c04d699dfc is described below

commit c04d699dfc56f6b577c72469f0b18581b868d562
Author: paul <p...@cohsoft.com.au>
AuthorDate: Wed Aug 2 10:53:29 2023 +1000

    Fixed: Prevent resource leaks in EntitySQLProcessor.groovy (OFBIZ-12836)
    
    Use try-with-resources to ensure the SQLProcessor is always closed.
---
 .../webtools/entity/EntitySQLProcessor.groovy      | 41 ++++++++++++----------
 1 file changed, 23 insertions(+), 18 deletions(-)

diff --git 
a/framework/webtools/src/main/groovy/org/apache/ofbiz/webtools/entity/EntitySQLProcessor.groovy
 
b/framework/webtools/src/main/groovy/org/apache/ofbiz/webtools/entity/EntitySQLProcessor.groovy
index 1f34a99994..72feb059b5 100644
--- 
a/framework/webtools/src/main/groovy/org/apache/ofbiz/webtools/entity/EntitySQLProcessor.groovy
+++ 
b/framework/webtools/src/main/groovy/org/apache/ofbiz/webtools/entity/EntitySQLProcessor.groovy
@@ -18,46 +18,49 @@
 */
 package org.apache.ofbiz.webtools.entity
 
+import java.sql.ResultSet
+import java.sql.ResultSetMetaData
+
 import org.apache.ofbiz.entity.jdbc.SQLProcessor
+import org.apache.ofbiz.entity.model.ModelGroupReader
 
-sqlCommand = context.request.getParameter('sqlCommand')
+String sqlCommand = context.request.getParameter('sqlCommand')
 
-resultMessage = ''
-rs = null
-columns = []
-records = []
-mgr = delegator.getModelGroupReader()
-groups = []
+String resultMessage = ''
+List<String> columns = []
+List<List<Object>> records = []
+ModelGroupReader mgr = delegator.getModelGroupReader()
+List<Map<String,String>> groups = []
 mgr.getGroupNames(delegator.getDelegatorName()).each { String group ->
     groups.add(0, ['group': group]) //use for list-option in widget drop-down
 }
 
 if (sqlCommand && selGroup) {
-    du = new SQLProcessor(delegator, delegator.getGroupHelperInfo(selGroup))
-    try {
+    try (SQLProcessor du = new SQLProcessor(delegator, 
delegator.getGroupHelperInfo(selGroup))) {
         if (sqlCommand.toUpperCase().startsWith('SELECT')) {
-            rs = du.executeQuery(sqlCommand)
-            if (rs) {
-                rsmd = rs.getMetaData()
-                numberOfColumns = rsmd.getColumnCount()
+            try (ResultSet rs = du.executeQuery(sqlCommand)) {
+                ResultSetMetaData rsmd = rs.getMetaData()
+
+                int numberOfColumns = rsmd.getColumnCount()
                 for (i = 1; i <= numberOfColumns; i++) {
                     columns.add(rsmd.getColumnLabel(i))
                 }
-                rowLimitReached = false
+
+                boolean rowLimitReached = false
                 while (rs.next()) {
                     if (records.size() >= rowLimit) {
-                        resultMessage = "Returned top $rowLimit rows."
                         rowLimitReached = true
                         break
                     }
-                    record = []
+
+                    List<Object> record = []
                     for (i = 1; i <= numberOfColumns; i++) {
                         record.add(rs.getObject(i))
                     }
                     records.add(record)
                 }
-                resultMessage = 'Returned ' + (rowLimitReached ? 'top ' + 
rowLimit : records.size() as String) + ' rows.'
-                rs.close()
+
+                resultMessage = "Returned ${rowLimitReached ? '' : 'top'} 
${records.size() as String} rows."
             }
         } else {
             if 
(sqlCommand.toUpperCase().contains('SYSCS_UTIL.SYSCS_EXPORT_TABLE')
@@ -69,6 +72,7 @@ if (sqlCommand && selGroup) {
                 context.sqlCommand = sqlCommand
                 return
             }
+
             du.prepareStatement(sqlCommand)
             numOfAffectedRows = du.executeUpdate()
             resultMessage = "Affected $numOfAffectedRows rows."
@@ -77,6 +81,7 @@ if (sqlCommand && selGroup) {
         resultMessage = exc.getMessage()
     }
 }
+
 context.groups = groups
 context.resultMessage = resultMessage
 context.columns = columns

Reply via email to