* FR 2214883 - minor improvement
* properly close ResultSets
This commit is contained in:
teo_sarca 2008-11-13 13:41:51 +00:00
parent 516ee8b439
commit 295f01f2e8
1 changed files with 70 additions and 113 deletions

View File

@ -20,12 +20,14 @@ package org.compiere.model;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level; import java.util.logging.Level;
import org.adempiere.exceptions.DBException;
import org.compiere.Adempiere; import org.compiere.Adempiere;
import org.compiere.util.CLogger; import org.compiere.util.CLogger;
import org.compiere.util.DB; import org.compiere.util.DB;
@ -137,6 +139,7 @@ public class MCost extends X_M_Cost
+ " AND (ce.CostingMethod IS NULL OR ce.CostingMethod=?) " // #7 + " AND (ce.CostingMethod IS NULL OR ce.CostingMethod=?) " // #7
+ "GROUP BY ce.CostElementType, ce.CostingMethod, c.Percent, c.M_CostElement_ID"; + "GROUP BY ce.CostElementType, ce.CostingMethod, c.Percent, c.M_CostElement_ID";
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try try
{ {
pstmt = DB.prepareStatement (sql, trxName); pstmt = DB.prepareStatement (sql, trxName);
@ -147,7 +150,7 @@ public class MCost extends X_M_Cost
pstmt.setInt (5, M_CostType_ID); pstmt.setInt (5, M_CostType_ID);
pstmt.setInt (6, as.getC_AcctSchema_ID()); pstmt.setInt (6, as.getC_AcctSchema_ID());
pstmt.setString (7, costingMethod); pstmt.setString (7, costingMethod);
ResultSet rs = pstmt.executeQuery (); rs = pstmt.executeQuery ();
while (rs.next ()) while (rs.next ())
{ {
currentCostPrice = rs.getBigDecimal(1); currentCostPrice = rs.getBigDecimal(1);
@ -171,23 +174,15 @@ public class MCost extends X_M_Cost
percentage = percentage.add(percent); percentage = percentage.add(percent);
count++; count++;
} }
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) DB.close(rs, pstmt);
pstmt.close (); rs = null; pstmt = null;
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
} }
if (count > 1) // Print summary if (count > 1) // Print summary
@ -498,6 +493,7 @@ public class MCost extends X_M_Cost
sql += " ORDER BY o.DateOrdered DESC, ol.Line DESC"; sql += " ORDER BY o.DateOrdered DESC, ol.Line DESC";
// //
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try try
{ {
pstmt = DB.prepareStatement (sql, product.get_TrxName()); pstmt = DB.prepareStatement (sql, product.get_TrxName());
@ -508,30 +504,22 @@ public class MCost extends X_M_Cost
pstmt.setInt (4, AD_Org_ID); pstmt.setInt (4, AD_Org_ID);
else if (M_ASI_ID != 0) else if (M_ASI_ID != 0)
pstmt.setInt(4, M_ASI_ID); pstmt.setInt(4, M_ASI_ID);
ResultSet rs = pstmt.executeQuery (); rs = pstmt.executeQuery ();
if (rs.next ()) if (rs.next ())
{ {
retValue = rs.getBigDecimal(1); retValue = rs.getBigDecimal(1);
if (retValue == null || retValue.signum() == 0) if (retValue == null || retValue.signum() == 0)
retValue = rs.getBigDecimal(2); retValue = rs.getBigDecimal(2);
} }
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) DB.close(rs, pstmt);
pstmt.close (); rs = null; pstmt = null;
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
} }
if (retValue != null) if (retValue != null)
@ -561,36 +549,29 @@ public class MCost extends X_M_Cost
+ " AND o.IsSOTrx='N'"; + " AND o.IsSOTrx='N'";
// //
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try try
{ {
pstmt = DB.prepareStatement (sql, product.get_TrxName()); pstmt = DB.prepareStatement (sql, product.get_TrxName());
pstmt.setInt (1, C_Currency_ID); pstmt.setInt (1, C_Currency_ID);
pstmt.setInt (2, C_Currency_ID); pstmt.setInt (2, C_Currency_ID);
pstmt.setInt (3, C_OrderLine_ID); pstmt.setInt (3, C_OrderLine_ID);
ResultSet rs = pstmt.executeQuery (); rs = pstmt.executeQuery ();
if (rs.next ()) if (rs.next ())
{ {
retValue = rs.getBigDecimal(1); retValue = rs.getBigDecimal(1);
if (retValue == null || retValue.signum() == 0) if (retValue == null || retValue.signum() == 0)
retValue = rs.getBigDecimal(2); retValue = rs.getBigDecimal(2);
} }
rs.close ();
pstmt.close ();
pstmt = null;
} }
catch (Exception e) catch (Exception e)
{ {
s_log.log (Level.SEVERE, sql, e); s_log.log (Level.SEVERE, sql, e);
} }
try finally
{ {
if (pstmt != null) DB.close(rs, pstmt);
pstmt.close (); rs = null; pstmt = null;
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
} }
if (retValue != null) if (retValue != null)
@ -739,8 +720,10 @@ public class MCost extends X_M_Cost
} // for all orgs } // for all orgs
} }
else else
{
s_log.warning("Not created: Std.Cost for " + product.getName() s_log.warning("Not created: Std.Cost for " + product.getName()
+ " - Costing Level on Batch/Lot"); + " - Costing Level on Batch/Lot");
}
} // accounting schema loop } // accounting schema loop
} // create } // create
@ -771,6 +754,7 @@ public class MCost extends X_M_Cost
sql += " ORDER BY t.M_Transaction_ID"; sql += " ORDER BY t.M_Transaction_ID";
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
BigDecimal newStockQty = Env.ZERO; BigDecimal newStockQty = Env.ZERO;
// //
BigDecimal newAverageAmt = Env.ZERO; BigDecimal newAverageAmt = Env.ZERO;
@ -783,7 +767,7 @@ public class MCost extends X_M_Cost
pstmt.setInt (2, AD_Org_ID); pstmt.setInt (2, AD_Org_ID);
else if (M_AttributeSetInstance_ID != 0) else if (M_AttributeSetInstance_ID != 0)
pstmt.setInt (2, M_AttributeSetInstance_ID); pstmt.setInt (2, M_AttributeSetInstance_ID);
ResultSet rs = pstmt.executeQuery (); rs = pstmt.executeQuery ();
while (rs.next ()) while (rs.next ())
{ {
BigDecimal oldStockQty = newStockQty; BigDecimal oldStockQty = newStockQty;
@ -819,23 +803,15 @@ public class MCost extends X_M_Cost
s_log.finer("Movement=" + movementQty + ", StockQty=" + newStockQty s_log.finer("Movement=" + movementQty + ", StockQty=" + newStockQty
+ ", Match=" + matchQty + ", Cost=" + cost + ", NewAvg=" + newAverageAmt); + ", Match=" + matchQty + ", Cost=" + cost + ", NewAvg=" + newAverageAmt);
} }
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) DB.close(rs, pstmt);
pstmt.close (); rs = null; pstmt = null;
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
} }
// //
if (newAverageAmt != null && newAverageAmt.signum() != 0) if (newAverageAmt != null && newAverageAmt.signum() != 0)
@ -872,6 +848,7 @@ public class MCost extends X_M_Cost
sql += " ORDER BY t.M_Transaction_ID"; sql += " ORDER BY t.M_Transaction_ID";
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
BigDecimal newStockQty = Env.ZERO; BigDecimal newStockQty = Env.ZERO;
// //
BigDecimal newAverageAmt = Env.ZERO; BigDecimal newAverageAmt = Env.ZERO;
@ -884,7 +861,7 @@ public class MCost extends X_M_Cost
pstmt.setInt (2, AD_Org_ID); pstmt.setInt (2, AD_Org_ID);
else if (M_AttributeSetInstance_ID != 0) else if (M_AttributeSetInstance_ID != 0)
pstmt.setInt (2, M_AttributeSetInstance_ID); pstmt.setInt (2, M_AttributeSetInstance_ID);
ResultSet rs = pstmt.executeQuery (); rs = pstmt.executeQuery ();
while (rs.next ()) while (rs.next ())
{ {
BigDecimal oldStockQty = newStockQty; BigDecimal oldStockQty = newStockQty;
@ -922,23 +899,15 @@ public class MCost extends X_M_Cost
s_log.finer("Movement=" + movementQty + ", StockQty=" + newStockQty s_log.finer("Movement=" + movementQty + ", StockQty=" + newStockQty
+ ", Match=" + matchQty + ", Cost=" + cost + ", NewAvg=" + newAverageAmt); + ", Match=" + matchQty + ", Cost=" + cost + ", NewAvg=" + newAverageAmt);
} }
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) DB.close(rs, pstmt);
pstmt.close (); rs = null; pstmt = null;
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
} }
// //
if (newAverageAmt != null && newAverageAmt.signum() != 0) if (newAverageAmt != null && newAverageAmt.signum() != 0)
@ -974,6 +943,7 @@ public class MCost extends X_M_Cost
sql += " ORDER BY t.M_Transaction_ID"; sql += " ORDER BY t.M_Transaction_ID";
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
// //
int oldTransaction_ID = 0; int oldTransaction_ID = 0;
ArrayList<QtyCost> fifo = new ArrayList<QtyCost>(); ArrayList<QtyCost> fifo = new ArrayList<QtyCost>();
@ -985,7 +955,7 @@ public class MCost extends X_M_Cost
pstmt.setInt (2, AD_Org_ID); pstmt.setInt (2, AD_Org_ID);
else if (M_AttributeSetInstance_ID != 0) else if (M_AttributeSetInstance_ID != 0)
pstmt.setInt (2, M_AttributeSetInstance_ID); pstmt.setInt (2, M_AttributeSetInstance_ID);
ResultSet rs = pstmt.executeQuery (); rs = pstmt.executeQuery ();
while (rs.next ()) while (rs.next ())
{ {
BigDecimal movementQty = rs.getBigDecimal(1); BigDecimal movementQty = rs.getBigDecimal(1);
@ -1065,26 +1035,21 @@ public class MCost extends X_M_Cost
} }
s_log.finer("Movement=" + movementQty + ", Size=" + fifo.size()); s_log.finer("Movement=" + movementQty + ", Size=" + fifo.size());
} }
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) DB.close(rs, pstmt);
pstmt.close (); rs = null; pstmt = null;
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
} }
if (fifo.size() == 0) if (fifo.size() == 0)
{
return null; return null;
}
QtyCost pp = (QtyCost)fifo.get(0); QtyCost pp = (QtyCost)fifo.get(0);
s_log.finer(product.getName() + " = " + pp.Cost); s_log.finer(product.getName() + " = " + pp.Cost);
return pp.Cost; return pp.Cost;
@ -1116,6 +1081,7 @@ public class MCost extends X_M_Cost
sql += " ORDER BY t.M_Transaction_ID DESC"; sql += " ORDER BY t.M_Transaction_ID DESC";
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
// //
int oldTransaction_ID = 0; int oldTransaction_ID = 0;
ArrayList<QtyCost> lifo = new ArrayList<QtyCost>(); ArrayList<QtyCost> lifo = new ArrayList<QtyCost>();
@ -1127,7 +1093,7 @@ public class MCost extends X_M_Cost
pstmt.setInt (2, AD_Org_ID); pstmt.setInt (2, AD_Org_ID);
else if (M_AttributeSetInstance_ID != 0) else if (M_AttributeSetInstance_ID != 0)
pstmt.setInt (2, M_AttributeSetInstance_ID); pstmt.setInt (2, M_AttributeSetInstance_ID);
ResultSet rs = pstmt.executeQuery (); rs = pstmt.executeQuery ();
while (rs.next ()) while (rs.next ())
{ {
BigDecimal movementQty = rs.getBigDecimal(1); BigDecimal movementQty = rs.getBigDecimal(1);
@ -1188,26 +1154,21 @@ public class MCost extends X_M_Cost
lifo.add(pp); lifo.add(pp);
s_log.finer("Movement=" + movementQty + ", Size=" + lifo.size()); s_log.finer("Movement=" + movementQty + ", Size=" + lifo.size());
} }
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) DB.close(rs, pstmt);
pstmt.close (); rs = null; pstmt = null;
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
} }
if (lifo.size() == 0) if (lifo.size() == 0)
{
return null; return null;
}
QtyCost pp = (QtyCost)lifo.get(lifo.size()-1); QtyCost pp = (QtyCost)lifo.get(lifo.size()-1);
s_log.finer(product.getName() + " = " + pp.Cost); s_log.finer(product.getName() + " = " + pp.Cost);
return pp.Cost; return pp.Cost;
@ -1217,7 +1178,7 @@ public class MCost extends X_M_Cost
/************************************************************************** /**************************************************************************
* MCost Qty-Cost Pair * MCost Qty-Cost Pair
*/ */
static class QtyCost public static class QtyCost
{ {
/** /**
* Constructor * Constructor
@ -1301,22 +1262,18 @@ public class MCost extends X_M_Cost
int M_CostType_ID, int C_AcctSchema_ID, int M_CostElement_ID, int M_CostType_ID, int C_AcctSchema_ID, int M_CostElement_ID,
int M_AttributeSetInstance_ID) int M_AttributeSetInstance_ID)
{ {
MCost retValue = null; final String whereClause = "AD_Client_ID=? AND AD_Org_ID=?"
//FR: [ 2214883 ] Remove SQL code and Replace for Query - red1 +" AND "+COLUMNNAME_M_Product_ID+"=?"
String whereClause = "AD_Client_ID=? AND AD_Org_ID=? AND M_Product_ID=?" +" AND "+COLUMNNAME_M_CostType_ID+"=?"
+ " AND M_CostType_ID=? AND C_AcctSchema_ID=? AND M_CostElement_ID=?" +" AND "+COLUMNNAME_C_AcctSchema_ID+"=?"
+ " AND M_AttributeSetInstance_ID=?"; +" AND "+COLUMNNAME_M_CostElement_ID+"=?"
retValue = new Query(ctx, MCost.Table_Name, whereClause, null) +" AND "+COLUMNNAME_M_AttributeSetInstance_ID+"=?";
.setParameters(new Object[]{ final Object[] params = new Object[]{AD_Client_ID, AD_Org_ID, M_Product_ID,
AD_Client_ID, M_CostType_ID, C_AcctSchema_ID,
AD_Org_ID, M_CostElement_ID, M_AttributeSetInstance_ID};
M_Product_ID, return new Query(ctx, Table_Name, whereClause, null)
M_CostType_ID, .setParameters(params)
C_AcctSchema_ID,
M_CostElement_ID,
M_AttributeSetInstance_ID})
.first(); .first();
return retValue;
} // get } // get
/** /**