Displaying differences for changeset
 
display as  

src/java/m/wqm/pestpractscores/V1_0.java

@@ -16,6 +16,7 @@
 import java.util.ArrayList;
 import java.util.Map;
 import java.util.logging.Level;
+import java.util.logging.Logger;
 import javax.ws.rs.Path;
 import oms3.annotations.Description;
 import oms3.annotations.Name;
@@ -90,8 +91,6 @@
         private int pdrift_practice_score;
         private ArrayList<ipmData> ipmList;
         private V1_0.Result results = null;
-        Connection conn = null;
-        Statement statement = null;
 
         PestPractScores(int AoAId, JSONArray ipms) {
             this.pleach_practice_score = this.psolsurf_practice_score = this.padsurf_practice_score = this.pdrift_practice_score = 0;
@@ -130,13 +129,6 @@
 
             if ((this.error_msg.isEmpty()) && ((null != this.ipmList) && (!this.ipmList.isEmpty()))) {
                 //Compute values here.
-                try {
-                    this.conn = wqm.utils.WQMTools.getConnection("wqm", LOG);
-                    this.statement = this.conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
-                } catch (ServiceException | SQLException se) {
-                    this.error_msg += " Cannot connect to database:  " + se.getMessage() + " ";
-                    LOG.log(Level.SEVERE, this.error_msg);
-                }
 
                 if (this.error_msg.isEmpty()) {
                     this.pleach_practice_score = this.psolsurf_practice_score = this.padsurf_practice_score = this.pdrift_practice_score = 0;
@@ -156,26 +148,19 @@
                     }
                 }
 
-                if (this.statement != null) {
-                    this.statement.close();
-                    this.statement = null;
-                }
-
-                if (this.conn != null) {
-                    this.conn.close();
-                    this.conn = null;
-                }
             }
 
             return ret_val;
         }
 
-        private int getPlPractScore(ipmData ipm) {
+        private int getPlPractScore(ipmData ipm) throws ServiceException {
             int ret_val = 0;
             String query;
 
             if (this.error_msg.isEmpty()) {
-                try {
+                try (
+                        Connection conn = wqm.utils.WQMTools.getConnection("wqm", LOG);
+                        Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);) {
                     String variant = ipm.getVariant();
                     query = "SELECT ipm_practice_score FROM wqm_ipm_practice_scores WHERE practice_id='" + ipm.getIpm() + "' AND wqm_concern='Pesticide Leaching'";
 
@@ -197,12 +182,14 @@
             return ret_val;
         }
 
-        private int getPsPractScore(ipmData ipm) {
+        private int getPsPractScore(ipmData ipm) throws ServiceException {
             int ret_val = 0;
             String query;
 
             if (this.error_msg.isEmpty()) {
-                try {
+                try (
+                        Connection conn = wqm.utils.WQMTools.getConnection("wqm", LOG);
+                        Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);) {
                     String variant = ipm.getVariant();
                     query = "SELECT ipm_practice_score FROM wqm_ipm_practice_scores WHERE practice_id='" + ipm.getIpm() + "' AND wqm_concern='Pesticide Solution Runoff'";
 
@@ -229,7 +216,9 @@
             String query;
 
             if (this.error_msg.isEmpty()) {
-                try {
+                try (
+                        Connection conn = wqm.utils.WQMTools.getConnection("wqm", LOG);
+                        Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);) {
                     String variant = ipm.getVariant();
                     query = "SELECT ipm_practice_score FROM wqm_ipm_practice_scores WHERE practice_id='" + ipm.getIpm() + "' AND wqm_concern='Pesticide Adsorbed Runoff'";
 
@@ -245,18 +234,22 @@
 
                 } catch (SQLException ex) {
                     this.error_msg += " Cannot compute pa_pract_score:  " + ex.getMessage() + " ";
+                } catch (ServiceException ex) {
+                    Logger.getLogger(V1_0.class.getName()).log(Level.SEVERE, null, ex);
                 }
             }
 
             return ret_val;
         }
 
-        private int getPdPractScore(ipmData ipm) {
+        private int getPdPractScore(ipmData ipm) throws ServiceException {
             int ret_val = 0;
             String query;
 
             if (this.error_msg.isEmpty()) {
-                try {
+                try (
+                        Connection conn = wqm.utils.WQMTools.getConnection("wqm", LOG);
+                        Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);) {
                     String variant = ipm.getVariant();
                     query = "SELECT ipm_practice_score FROM wqm_ipm_practice_scores WHERE practice_id='" + ipm.getIpm() + "' AND wqm_concern='Pesticide Drift'";