Displaying differences for changeset
 
display as  

src/java/m/wqm/sednutpractscores/V1_0.java

@@ -13,6 +13,7 @@
 import java.util.Map;
 import java.sql.*;
 import java.util.logging.Level;
+import java.util.logging.Logger;
 import org.codehaus.jettison.json.JSONException;
 
 @Name("WQM-15: Sediment and Nutrient Practice Scores (SedNutPractScores)")
@@ -26,9 +27,6 @@
  */
 public class V1_0 extends ModelDataService {
 
-    private Connection conn;
-    private Statement statement;
-
     private ArrayList<V1_0.Input> components = new ArrayList<>(); // store the set of all input soilcomponents as objects
     private ArrayList<V1_0.Result1> result1 = new ArrayList<>();  // store the result as objects
     private int nleach_pract_score = 0;
@@ -75,8 +73,6 @@
         this.result1 = new ArrayList<>();
 
         try {
-            conn = wqm.utils.WQMTools.getConnection("wqm", LOG);
-            statement = conn.createStatement();
 
             JSONArray groups = getJSONArrayParam("pestcomponents");
             for (int i = 0; i < groups.length(); i++) {
@@ -88,7 +84,10 @@
                 String plan_pract_discrim_value = JSONUtils.getStringParam(group, "plan_practice_discrim_value", "err");
                 components.add(new V1_0.Input(AoAid, plan_pract_id, plan_pract_discrim_type, plan_pract_discrim_value));
             }
-        } catch (ServiceException | SQLException | JSONException ex) {
+        } catch (JSONException ex) {
+            this.error_msg = "Cannot process the input JSON: " + ex.getMessage();
+            LOG.log(Level.SEVERE, this.error_msg);
+        } catch (ServiceException ex) {
             this.error_msg = "Cannot process the input JSON: " + ex.getMessage();
             LOG.log(Level.SEVERE, this.error_msg);
         }
@@ -97,7 +96,9 @@
     @Override
     protected String process() throws Exception {
         if (this.error_msg.isEmpty()) {
-            try {
+            try (
+                    Connection conn = wqm.utils.WQMTools.getConnection("wqm", LOG);
+                    Statement statement = conn.createStatement();) {
                 for (V1_0.Input ip : components) {
                     String query;
                     //  #Compute practice mitigation scores for Nitrogen in Ground Water and increment total scores
@@ -258,16 +259,6 @@
             } catch (NumberFormatException | SQLException ex) {
                 this.error_msg = "Cannot process that request: " + ex.getMessage();
                 LOG.log(Level.SEVERE, this.error_msg);
-            } finally {
-                if (this.statement != null) {
-                    this.statement.close();
-                    this.statement = null;
-                }
-
-                if (this.conn != null) {
-                    this.conn.close();
-                    this.conn = null;
-                }
             }
         }
         return (this.error_msg.isEmpty() ? EXEC_OK : this.error_msg);