From d50f0c102c58f02c2dac5b84885c82d3657648ac Mon Sep 17 00:00:00 2001 From: teo_sarca Date: Thu, 11 Jun 2009 07:18:15 +0000 Subject: [PATCH] MUOMConversion: * BF [ 1874419 ] JDBC Statement not close in a finally block https://sourceforge.net/tracker/?func=detail&aid=1874419&group_id=176962&atid=879332 * use new Query API when possible * minor error reporting problem fix --- .../org/compiere/model/MUOMConversion.java | 86 +++++++------------ 1 file changed, 31 insertions(+), 55 deletions(-) diff --git a/base/src/org/compiere/model/MUOMConversion.java b/base/src/org/compiere/model/MUOMConversion.java index 7b2d53a0f3..369dc3aa6f 100644 --- a/base/src/org/compiere/model/MUOMConversion.java +++ b/base/src/org/compiere/model/MUOMConversion.java @@ -25,9 +25,11 @@ import java.sql.Timestamp; import java.util.ArrayList; import java.util.Calendar; import java.util.GregorianCalendar; +import java.util.List; import java.util.Properties; import java.util.logging.Level; +import org.adempiere.exceptions.DBException; import org.compiere.util.CCache; import org.compiere.util.CLogger; import org.compiere.util.DB; @@ -174,10 +176,12 @@ public class MUOMConversion extends X_C_UOM_Conversion + "FROM C_UOM_Conversion " + "WHERE IsActive='Y' AND M_Product_ID IS NULL", "C_UOM_Conversion", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO); + PreparedStatement pstmt = null; + ResultSet rs = null; try { - PreparedStatement pstmt = DB.prepareStatement(sql, null); - ResultSet rs = pstmt.executeQuery(); + pstmt = DB.prepareStatement(sql, null); + rs = pstmt.executeQuery(); while (rs.next()) { Point p = new Point (rs.getInt(1), rs.getInt(2)); @@ -191,13 +195,16 @@ public class MUOMConversion extends X_C_UOM_Conversion if (dr != null) s_conversions.put(new Point(p.y,p.x), dr); } - rs.close(); - pstmt.close(); } catch (SQLException e) { s_log.log(Level.SEVERE, sql, e); } + finally + { + DB.close(rs, pstmt); + rs = null; pstmt = null; + } } // createRatess /** @@ -403,39 +410,31 @@ public class MUOMConversion extends X_C_UOM_Conversion + " AND c.M_Product_ID IS NULL" + " ORDER BY c.AD_Client_ID DESC, c.AD_Org_ID DESC"; PreparedStatement pstmt = null; + ResultSet rs = null; try { pstmt = DB.prepareStatement(sql, null); pstmt.setInt(1, C_UOM_From_ID); pstmt.setInt(2, C_UOM_To_ID); - ResultSet rs = pstmt.executeQuery(); + rs = pstmt.executeQuery(); if (rs.next()) { retValue = rs.getBigDecimal(1); precision = rs.getInt(StdPrecision ? 2 : 3); } - rs.close(); - pstmt.close(); - pstmt = null; } - catch (Exception e) + catch (SQLException e) { - s_log.log(Level.SEVERE, sql, e); + throw new DBException(e, sql); } - try + finally { - if (pstmt != null) - pstmt.close(); - pstmt = null; - } - catch (Exception e) - { - pstmt = null; + DB.close(rs, pstmt); + rs = null; pstmt = null; } if (retValue == null) { - s_log.info ("NOT found - FromUOM=" + C_UOM_From_ID - + ", ToUOM=" + C_UOM_To_ID); + s_log.info ("NOT found - FromUOM=" + C_UOM_From_ID + ", ToUOM=" + C_UOM_To_ID); return null; } @@ -601,37 +600,14 @@ public class MUOMConversion extends X_C_UOM_Conversion MUOMConversion defRate = new MUOMConversion (MProduct.get(ctx, M_Product_ID)); list.add(defRate); // - String sql = "SELECT * FROM C_UOM_Conversion c " - + "WHERE c.M_Product_ID=?" - + " AND EXISTS (SELECT * FROM M_Product p " - + "WHERE c.M_Product_ID=p.M_Product_ID AND c.C_UOM_ID=p.C_UOM_ID)" - + " AND c.IsActive='Y'"; - PreparedStatement pstmt = null; - try - { - pstmt = DB.prepareStatement (sql, null); - pstmt.setInt (1, M_Product_ID); - ResultSet rs = pstmt.executeQuery (); - while (rs.next ()) - list.add(new MUOMConversion(ctx, rs, null)); - rs.close (); - pstmt.close (); - pstmt = null; - } - catch (Exception e) - { - s_log.log(Level.SEVERE, sql, e); - } - try - { - if (pstmt != null) - pstmt.close (); - pstmt = null; - } - catch (Exception e) - { - pstmt = null; - } + final String whereClause = "M_Product_ID=?" + + " AND EXISTS (SELECT 1 FROM M_Product p " + + "WHERE C_UOM_Conversion.M_Product_ID=p.M_Product_ID AND C_UOM_Conversion.C_UOM_ID=p.C_UOM_ID)"; + List conversions = new Query(ctx, Table_Name, whereClause, null) + .setParameters(new Object[]{M_Product_ID}) + .setOnlyActiveRecords(true) + .list(); + list.addAll(conversions); // Convert & save result = new MUOMConversion[list.size ()]; @@ -642,13 +618,13 @@ public class MUOMConversion extends X_C_UOM_Conversion } // getProductConversions /** Static Logger */ - private static CLogger s_log = CLogger.getCLogger(MUOMConversion.class); + private static final CLogger s_log = CLogger.getCLogger(MUOMConversion.class); /** Indicator for Rate */ - private static BigDecimal GETRATE = new BigDecimal(123.456); + private static final BigDecimal GETRATE = new BigDecimal(123.456); /** Conversion Map: Key=Point(from,to) Value=BigDecimal */ private static CCache s_conversions = null; /** Product Conversion Map */ - private static CCache s_conversionProduct + private static final CCache s_conversionProduct = new CCache("C_UOMConversion", 20); @@ -716,7 +692,7 @@ public class MUOMConversion extends X_C_UOM_Conversion // From - To is the same if (getC_UOM_ID() == getC_UOM_To_ID()) { - log.saveError("Error", Msg.parseTranslation(getCtx(), "@C_UOM_ID@ = @C_UOM_ID@")); + log.saveError("Error", Msg.parseTranslation(getCtx(), "@C_UOM_ID@ = @C_UOM_To_ID@")); return false; } // Nothing to convert