From 5542d14a95cf8b28abfbbab6709e9ed8e628fea1 Mon Sep 17 00:00:00 2001 From: teo_sarca Date: Mon, 30 Jun 2008 07:03:14 +0000 Subject: [PATCH] org.compiere.acct: fix db connection --- base/src/org/compiere/acct/Doc.java | 12 ++-- .../src/org/compiere/acct/Doc_Allocation.java | 36 +++------- base/src/org/compiere/acct/Doc_Invoice.java | 13 ++-- base/src/org/compiere/acct/Doc_Order.java | 13 ++-- .../org/compiere/acct/Doc_ProjectIssue.java | 3 - base/src/org/compiere/acct/FactLine.java | 72 ++++++++++++------- base/src/org/compiere/acct/Matcher.java | 12 ++-- 7 files changed, 90 insertions(+), 71 deletions(-) diff --git a/base/src/org/compiere/acct/Doc.java b/base/src/org/compiere/acct/Doc.java index 6adf26a0af..3362510f79 100644 --- a/base/src/org/compiere/acct/Doc.java +++ b/base/src/org/compiere/acct/Doc.java @@ -1469,9 +1469,11 @@ public abstract class Doc // Get Acct int Account_ID = 0; + PreparedStatement pstmt = null; + ResultSet rs = null; try { - PreparedStatement pstmt = DB.prepareStatement(sql, null); + pstmt = DB.prepareStatement(sql, null); if (para_1 == -1) // GL Accounts pstmt.setInt (1, as.getC_AcctSchema_ID()); else @@ -1479,17 +1481,19 @@ public abstract class Doc pstmt.setInt (1, para_1); pstmt.setInt (2, as.getC_AcctSchema_ID()); } - ResultSet rs = pstmt.executeQuery(); + rs = pstmt.executeQuery(); if (rs.next()) Account_ID = rs.getInt(1); - rs.close(); - pstmt.close(); } catch (SQLException e) { log.log(Level.SEVERE, "AcctType=" + AcctType + " - SQL=" + sql, e); return 0; } + finally { + DB.close(rs, pstmt); + rs = null; pstmt = null; + } // No account if (Account_ID == 0) { diff --git a/base/src/org/compiere/acct/Doc_Allocation.java b/base/src/org/compiere/acct/Doc_Allocation.java index 16181041b9..007c2881a2 100644 --- a/base/src/org/compiere/acct/Doc_Allocation.java +++ b/base/src/org/compiere/acct/Doc_Allocation.java @@ -710,34 +710,26 @@ public class Doc_Allocation extends Doc + " AND PostingType='A'"; //AND C_Currency_ID=102 PreparedStatement pstmt = null; + ResultSet rs = null; try { pstmt = DB.prepareStatement(sql, getTrxName()); pstmt.setInt(1, invoice.getC_Invoice_ID()); pstmt.setInt(2, as.getC_AcctSchema_ID()); - ResultSet rs = pstmt.executeQuery(); + rs = pstmt.executeQuery(); if (rs.next()) { invoiceSource = rs.getBigDecimal(1); invoiceAccounted = rs.getBigDecimal(2); } - rs.close(); - pstmt.close(); - pstmt = null; } catch (Exception e) { log.log(Level.SEVERE, sql, e); } - try - { - if (pstmt != null) - pstmt.close(); - pstmt = null; - } - catch (Exception e) - { - pstmt = null; + finally { + DB.close(rs, pstmt); + rs = null; pstmt = null; } // Requires that Invoice is Posted if (invoiceSource == null || invoiceAccounted == null) @@ -859,31 +851,23 @@ public class Doc_Allocation extends Doc + " AND C_AcctSchema_ID=?" + " AND Line_ID IS NULL"; // header lines like tax or total PreparedStatement pstmt = null; + ResultSet rs = null; try { pstmt = DB.prepareStatement(sql, getTrxName()); pstmt.setInt(1, line.getC_Invoice_ID()); pstmt.setInt(2, as.getC_AcctSchema_ID()); - ResultSet rs = pstmt.executeQuery(); + rs = pstmt.executeQuery(); while (rs.next()) tax.addInvoiceFact (new MFactAcct(getCtx(), rs, fact.get_TrxName())); - rs.close(); - pstmt.close(); - pstmt = null; } catch (Exception e) { log.log(Level.SEVERE, sql, e); } - try - { - if (pstmt != null) - pstmt.close(); - pstmt = null; - } - catch (Exception e) - { - pstmt = null; + finally { + DB.close(rs, pstmt); + rs = null; pstmt = null; } // Invoice Not posted if (tax.getLineCount() == 0) diff --git a/base/src/org/compiere/acct/Doc_Invoice.java b/base/src/org/compiere/acct/Doc_Invoice.java index 9e0c308cd3..2564323409 100644 --- a/base/src/org/compiere/acct/Doc_Invoice.java +++ b/base/src/org/compiere/acct/Doc_Invoice.java @@ -85,11 +85,13 @@ public class Doc_Invoice extends Doc String sql = "SELECT it.C_Tax_ID, t.Name, t.Rate, it.TaxBaseAmt, it.TaxAmt, t.IsSalesTax " + "FROM C_Tax t, C_InvoiceTax it " + "WHERE t.C_Tax_ID=it.C_Tax_ID AND it.C_Invoice_ID=?"; + PreparedStatement pstmt = null; + ResultSet rs = null; try { - PreparedStatement pstmt = DB.prepareStatement(sql, getTrxName()); + pstmt = DB.prepareStatement(sql, getTrxName()); pstmt.setInt(1, get_ID()); - ResultSet rs = pstmt.executeQuery(); + rs = pstmt.executeQuery(); // while (rs.next()) { @@ -105,15 +107,16 @@ public class Doc_Invoice extends Doc log.fine(taxLine.toString()); list.add(taxLine); } - // - rs.close(); - pstmt.close(); } catch (SQLException e) { log.log(Level.SEVERE, sql, e); return null; } + finally { + DB.close(rs, pstmt); + rs = null; pstmt = null; + } // Return Array DocTax[] tl = new DocTax[list.size()]; diff --git a/base/src/org/compiere/acct/Doc_Order.java b/base/src/org/compiere/acct/Doc_Order.java index eb87b05f01..322c9173a8 100644 --- a/base/src/org/compiere/acct/Doc_Order.java +++ b/base/src/org/compiere/acct/Doc_Order.java @@ -237,11 +237,13 @@ public class Doc_Order extends Doc String sql = "SELECT it.C_Tax_ID, t.Name, t.Rate, it.TaxBaseAmt, it.TaxAmt, t.IsSalesTax " + "FROM C_Tax t, C_OrderTax it " + "WHERE t.C_Tax_ID=it.C_Tax_ID AND it.C_Order_ID=?"; + PreparedStatement pstmt = null; + ResultSet rs = null; try { - PreparedStatement pstmt = DB.prepareStatement(sql, getTrxName()); + pstmt = DB.prepareStatement(sql, getTrxName()); pstmt.setInt(1, get_ID()); - ResultSet rs = pstmt.executeQuery(); + rs = pstmt.executeQuery(); // while (rs.next()) { @@ -256,13 +258,14 @@ public class Doc_Order extends Doc taxBaseAmt, amount, salesTax); list.add(taxLine); } - // - rs.close(); - pstmt.close(); } catch (SQLException e) { log.log(Level.SEVERE, sql, e); + } + finally { + DB.close(rs, pstmt); + rs = null; pstmt = null; } // Return Array diff --git a/base/src/org/compiere/acct/Doc_ProjectIssue.java b/base/src/org/compiere/acct/Doc_ProjectIssue.java index 9a79b8a2e6..12a0a2daf0 100644 --- a/base/src/org/compiere/acct/Doc_ProjectIssue.java +++ b/base/src/org/compiere/acct/Doc_ProjectIssue.java @@ -228,9 +228,6 @@ public class Doc_ProjectIssue extends Doc } else log.warning("Not found for S_TimeExpenseLine_ID=" + m_issue.getS_TimeExpenseLine_ID()); - rs.close(); - pstmt.close(); - pstmt = null; } catch (Exception e) { diff --git a/base/src/org/compiere/acct/FactLine.java b/base/src/org/compiere/acct/FactLine.java index bae4b5f045..4cd1143309 100644 --- a/base/src/org/compiere/acct/FactLine.java +++ b/base/src/org/compiere/acct/FactLine.java @@ -446,20 +446,24 @@ public final class FactLine extends X_Fact_Acct int C_Location_ID = 0; String sql = "SELECT w.C_Location_ID FROM M_Warehouse w, M_Locator l " + "WHERE w.M_Warehouse_ID=l.M_Warehouse_ID AND l.M_Locator_ID=?"; + PreparedStatement pstmt = null; + ResultSet rs = null; try { - PreparedStatement pstmt = DB.prepareStatement(sql, get_TrxName()); + pstmt = DB.prepareStatement(sql, get_TrxName()); pstmt.setInt(1, M_Locator_ID); - ResultSet rs = pstmt.executeQuery(); + rs = pstmt.executeQuery(); if (rs.next()) C_Location_ID = rs.getInt(1); - rs.close(); - pstmt.close(); } catch (SQLException e) { log.log(Level.SEVERE, sql, e); return; + } + finally { + DB.close(rs, pstmt); + rs = null; pstmt = null; } if (C_Location_ID != 0) setLocation (C_Location_ID, isFrom); @@ -476,20 +480,24 @@ public final class FactLine extends X_Fact_Acct return; int C_Location_ID = 0; String sql = "SELECT C_Location_ID FROM C_BPartner_Location WHERE C_BPartner_Location_ID=?"; + PreparedStatement pstmt = null; + ResultSet rs = null; try { - PreparedStatement pstmt = DB.prepareStatement(sql, get_TrxName()); + pstmt = DB.prepareStatement(sql, get_TrxName()); pstmt.setInt(1, C_BPartner_Location_ID); - ResultSet rs = pstmt.executeQuery(); + rs = pstmt.executeQuery(); if (rs.next()) C_Location_ID = rs.getInt(1); - rs.close(); - pstmt.close(); } catch (SQLException e) { log.log(Level.SEVERE, sql, e); return; + } + finally { + DB.close(rs, pstmt); + rs = null; pstmt = null; } if (C_Location_ID != 0) setLocation (C_Location_ID, isFrom); @@ -506,20 +514,24 @@ public final class FactLine extends X_Fact_Acct return; int C_Location_ID = 0; String sql = "SELECT C_Location_ID FROM AD_OrgInfo WHERE AD_Org_ID=?"; + PreparedStatement pstmt = null; + ResultSet rs = null; try { - PreparedStatement pstmt = DB.prepareStatement(sql, get_TrxName()); + pstmt = DB.prepareStatement(sql, get_TrxName()); pstmt.setInt(1, AD_Org_ID); - ResultSet rs = pstmt.executeQuery(); + rs = pstmt.executeQuery(); if (rs.next()) C_Location_ID = rs.getInt(1); - rs.close(); - pstmt.close(); } catch (SQLException e) { log.log(Level.SEVERE, sql, e); return; + } + finally { + DB.close(rs, pstmt); + rs = null; pstmt = null; } if (C_Location_ID != 0) setLocation (C_Location_ID, isFrom); @@ -691,12 +703,14 @@ public final class FactLine extends X_Fact_Acct if (getM_Locator_ID() != 0) { String sql = "SELECT AD_Org_ID FROM M_Locator WHERE M_Locator_ID=? AND AD_Client_ID=?"; + PreparedStatement pstmt = null; + ResultSet rs = null; try { - PreparedStatement pstmt = DB.prepareStatement(sql, get_TrxName()); + pstmt = DB.prepareStatement(sql, get_TrxName()); pstmt.setInt(1, getM_Locator_ID()); pstmt.setInt(2, getAD_Client_ID()); - ResultSet rs = pstmt.executeQuery(); + rs = pstmt.executeQuery(); if (rs.next()) { setAD_Org_ID (rs.getInt(1)); @@ -704,12 +718,14 @@ public final class FactLine extends X_Fact_Acct } else log.log(Level.SEVERE, "AD_Org_ID - Did not find M_Locator_ID=" + getM_Locator_ID()); - rs.close(); - pstmt.close(); } catch (SQLException e) { log.log(Level.SEVERE, sql, e); + } + finally { + DB.close(rs, pstmt); + rs = null; pstmt = null; } } // M_Locator_ID != 0 @@ -930,23 +946,27 @@ public final class FactLine extends X_Fact_Acct + "WHERE ga.C_BP_Group_ID=p.C_BP_Group_ID" + " AND ga.UnearnedRevenue_Acct=vc.C_ValidCombination_ID" + " AND ga.C_AcctSchema_ID=? AND p.C_BPartner_ID=?"; + PreparedStatement pstmt = null; + ResultSet rs = null; try { - PreparedStatement pstmt = DB.prepareStatement(sql, get_TrxName()); + pstmt = DB.prepareStatement(sql, get_TrxName()); pstmt.setInt(1, getC_AcctSchema_ID()); pstmt.setInt(2, C_BPartner_ID); - ResultSet rs = pstmt.executeQuery(); + rs = pstmt.executeQuery(); if (rs.next()) { UnearnedRevenue_Acct = rs.getInt(1); new_Account_ID = rs.getInt(2); } - rs.close(); - pstmt.close(); } catch (SQLException e) { log.log(Level.SEVERE, sql, e); + } + finally { + DB.close(rs, pstmt); + rs = null; pstmt = null; } if (new_Account_ID == 0) { @@ -992,15 +1012,17 @@ public final class FactLine extends X_Fact_Acct + "FROM Fact_Acct " + "WHERE C_AcctSchema_ID=? AND AD_Table_ID=? AND Record_ID=?" + " AND Line_ID=? AND Account_ID=?"; + PreparedStatement pstmt = null; + ResultSet rs = null; try { - PreparedStatement pstmt = DB.prepareStatement(sql, get_TrxName()); + pstmt = DB.prepareStatement(sql, get_TrxName()); pstmt.setInt(1, getC_AcctSchema_ID()); pstmt.setInt(2, AD_Table_ID); pstmt.setInt(3, Record_ID); pstmt.setInt(4, Line_ID); pstmt.setInt(5, m_acct.getAccount_ID()); - ResultSet rs = pstmt.executeQuery(); + rs = pstmt.executeQuery(); if (rs.next()) { MFactAcct fact = new MFactAcct(getCtx(), rs, get_TrxName()); @@ -1046,13 +1068,15 @@ public final class FactLine extends X_Fact_Acct .append(",Record_ID=").append(Record_ID) .append(",Line_ID=").append(Line_ID) .append(", Account_ID=").append(m_acct.getAccount_ID()).toString()); - rs.close(); - pstmt.close(); } catch (SQLException e) { log.log(Level.SEVERE, sql, e); } + finally { + DB.close(rs, pstmt); + rs = null; pstmt = null; + } return success; } // updateReverseLine diff --git a/base/src/org/compiere/acct/Matcher.java b/base/src/org/compiere/acct/Matcher.java index 0a7e8886b3..cf050faf05 100644 --- a/base/src/org/compiere/acct/Matcher.java +++ b/base/src/org/compiere/acct/Matcher.java @@ -86,11 +86,13 @@ public class Matcher // Not existing Inv Matches + " AND NOT EXISTS (SELECT * FROM M_MatchInv mi " + "WHERE mi.C_InvoiceLine_ID=m1.C_InvoiceLine_ID AND mi.M_InOutLine_ID=m2.M_InOutLine_ID)"; + PreparedStatement pstmt = null; + ResultSet rs = null; try { - PreparedStatement pstmt = DB.prepareStatement(sql, null); + pstmt = DB.prepareStatement(sql, null); pstmt.setInt(1, m_AD_Client_ID); - ResultSet rs = pstmt.executeQuery(); + rs = pstmt.executeQuery(); while (rs.next()) { BigDecimal qty1 = rs.getBigDecimal(8); @@ -115,13 +117,15 @@ public class Matcher M_Product_ID, DateTrx, Qty)) counter++; } - rs.close(); - pstmt.close(); } catch (SQLException e) { log.log(Level.SEVERE, "match", e); } + finally { + DB.close(rs, pstmt); + rs = null; pstmt = null; + } log.fine("Matcher.match - Client_ID=" + m_AD_Client_ID + ", Records created=" + counter); return counter;