IDEMPIERE-568 Review proper closing of JDBC statements and resultsets

This commit is contained in:
Richard Morales 2013-02-20 20:31:16 -05:00
parent b7acdff746
commit 65a9dc9adc
26 changed files with 184 additions and 204 deletions

View File

@ -97,13 +97,13 @@ public class InOutGenerateRMA extends SvrProcess
+ "AND T_Selection.AD_PInstance_ID=? ";
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement(sql, get_TrxName());
pstmt.setInt(1, Env.getAD_Client_ID(getCtx()));
pstmt.setInt(2, getAD_PInstance_ID());
ResultSet rs = pstmt.executeQuery();
rs = pstmt.executeQuery();
while (rs.next())
{
@ -116,14 +116,8 @@ public class InOutGenerateRMA extends SvrProcess
}
finally
{
try
{
pstmt.close();
}
catch (Exception ex)
{
log.log(Level.SEVERE, "Could not close prepared statement");
}
DB.close(rs,pstmt);
rs = null;pstmt = null;
}
StringBuilder msgreturn = new StringBuilder("@Created@ = ").append(m_created);

View File

@ -92,13 +92,13 @@ public class InvoiceGenerateRMA extends SvrProcess
+ "AND T_Selection.AD_PInstance_ID=? ";
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement(sql, get_TrxName());
pstmt.setInt(1, Env.getAD_Client_ID(getCtx()));
pstmt.setInt(2, getAD_PInstance_ID());
ResultSet rs = pstmt.executeQuery();
rs = pstmt.executeQuery();
while (rs.next())
{
@ -111,14 +111,8 @@ public class InvoiceGenerateRMA extends SvrProcess
}
finally
{
try
{
pstmt.close();
}
catch (Exception ex)
{
log.log(Level.SEVERE, "Could not close prepared statement");
}
DB.close(rs,pstmt);
rs = null;pstmt = null;
}
StringBuilder msgreturn = new StringBuilder("@Created@ = ").append(m_created);
return msgreturn.toString();

View File

@ -85,21 +85,23 @@ public class PrepareMigrationScripts extends SvrProcess {
.append(fileName.get(i))
.append("]. Finding out if the script has or hasn't been applied yet...");
log.fine(msglog.toString());
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// First of all, check if the script hasn't been applied yet...
String checkScript = "select ad_migrationscript_id from ad_migrationscript where name = ?";
PreparedStatement pstmt = DB.prepareStatement(checkScript, this
pstmt = DB.prepareStatement(checkScript, this
.get_TrxName());
pstmt.setString(1, fileName.get(i));
ResultSet rs = pstmt.executeQuery();
rs = pstmt.executeQuery();
if (rs.next()) {
msglog = new StringBuilder("Script ").append(fileName.get(i))
.append(" already in the database");
log.warning(msglog.toString());
pstmt.close();
continue;
}
pstmt.close();
DB.close(pstmt);
pstmt = null;
// first use a Scanner to get each line
Scanner scanner = new Scanner(dirList[i]);
StringBuilder body = new StringBuilder();
@ -213,7 +215,8 @@ public class PrepareMigrationScripts extends SvrProcess {
pstmt.setTimestamp(15, ts);
pstmt.setTimestamp(16, ts);
int result = pstmt.executeUpdate();
pstmt.close();
DB.close(pstmt);
pstmt = null;
if (result > 0)
log.info("Header inserted. Now inserting the script body");
else {
@ -227,7 +230,8 @@ public class PrepareMigrationScripts extends SvrProcess {
pstmt.setBytes(1, body.toString().getBytes());
pstmt.setInt(2, seqID);
result = pstmt.executeUpdate();
pstmt.close();
DB.close(pstmt);
pstmt = null;
if (result > 0)
log.info("Script Body inserted.");
else {
@ -245,6 +249,13 @@ public class PrepareMigrationScripts extends SvrProcess {
} catch (Exception ex) {
log.severe(ex.getMessage());
}
finally
{
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
}
return "Sucess";
}

View File

@ -80,11 +80,12 @@ public class UpdateRoleMenu extends SvrProcess
String sqlStmt = "SELECT U_WebMenu_ID, IsActive FROM U_WebMenu";
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement(sqlStmt, get_TrxName());
ResultSet rs = pstmt.executeQuery();
rs = pstmt.executeQuery();
while (rs.next())
{
@ -101,17 +102,9 @@ public class UpdateRoleMenu extends SvrProcess
}
finally
{
if (pstmt != null)
{
try
{
pstmt.close();
}
catch (Exception ex)
{
log.log(Level.SEVERE, "Could not close prepared statement");
}
}
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
return "Role updated successfully";

View File

@ -225,7 +225,6 @@ public class PaySelectionCreateFrom extends SvrProcess
PayAmt, PayAmt.subtract(DiscountAmt), DiscountAmt);
if (!pselLine.save())
{
pstmt.close();
throw new IllegalStateException ("Cannot save MPaySelectionLine");
}
}

View File

@ -80,13 +80,13 @@ public final class ProcessUtil {
public static boolean startDatabaseProcedure (ProcessInfo processInfo, String ProcedureName, Trx trx, boolean managedTrx) {
String sql = "{call " + ProcedureName + "(?)}";
String trxName = trx != null ? trx.getTrxName() : null;
CallableStatement cstmt = null;
try
{
//hengsin, add trx support, updateable support.
CallableStatement cstmt = DB.prepareCall(sql, ResultSet.CONCUR_UPDATABLE, trxName);
cstmt = DB.prepareCall(sql, ResultSet.CONCUR_UPDATABLE, trxName);
cstmt.setInt(1, processInfo.getAD_PInstance_ID());
cstmt.executeUpdate();
cstmt.close();
if (trx != null && trx.isActive() && managedTrx)
{
trx.commit(true);
@ -105,6 +105,8 @@ public final class ProcessUtil {
}
finally
{
DB.close(cstmt);
cstmt = null;
if (trx != null && managedTrx)
trx.close();
}

View File

@ -749,9 +749,6 @@ public class CreateAdempiere
log.finest("# " + no);
}
//
stmt.close();
stmt = null;
//
if (batch)
conn.commit();
//
@ -774,17 +771,11 @@ public class CreateAdempiere
msg += "\n=>" + cmd;
log.log(Level.SEVERE, msg);
}
// Error clean up
try
finally
{
if (stmt != null)
stmt.close();
DB.close(stmt);
stmt = null;
}
catch (SQLException e1)
{
log.log(Level.SEVERE, "close statement", e1);
}
stmt = null;
return false;
} // execureCommands

View File

@ -179,10 +179,7 @@ public class StatementProxy implements InvocationHandler {
if (close) return;
try {
if (p_stmt != null)
{
p_stmt.close();
}
DB.close(p_stmt);
} finally {
if (m_conn != null)
{
@ -194,6 +191,7 @@ public class StatementProxy implements InvocationHandler {
{}
}
m_conn = null;
p_stmt = null;
close = true;
}
} // close
@ -222,6 +220,9 @@ public class StatementProxy implements InvocationHandler {
finally
{
DB.close(rs);
rs = null;
DB.close(rowSet);
rowSet = null;
}
return rowSet;
} // local_getRowSet

View File

@ -97,16 +97,11 @@ public class MDepreciationConvention extends X_A_Depreciation_Convention
cs.setInt(6, Period);
cs.execute();
retValue = cs.getBigDecimal(1);
cs.close();
} catch (Exception e) {
log.log(Level.SEVERE, sql, e);
}
finally {
try {
if (cs != null) cs.close();
} catch (SQLException e) {
log.log(Level.FINEST, "Error", e);
}
DB.close(cs);
cs = null;
}
}

View File

@ -178,7 +178,6 @@ public class MDepreciationMethod extends X_A_Depreciation_Method
cs.setInt(6, A_Asset_Acct_ID);
cs.execute();
retValue = cs.getBigDecimal(1);
cs.close();
}
catch (SQLException e)
{

View File

@ -20,6 +20,7 @@ import java.sql.ResultSet;
import java.util.Properties;
import org.adempiere.exceptions.AdempiereException;
import org.compiere.util.DB;
import org.compiere.util.Env;
/**
@ -223,16 +224,18 @@ public class MElementValue extends X_C_ElementValue
//
// Check Valid Combinations - teo_sarca FR [ 1883533 ]
String whereClause = MAccount.COLUMNNAME_Account_ID+"=?";
POResultSet<MAccount> rs = new Query(getCtx(), I_C_ValidCombination.Table_Name, whereClause, get_TrxName())
.setParameters(get_ID())
.scroll();
POResultSet<MAccount> rs = null;
try {
rs = new Query(getCtx(), I_C_ValidCombination.Table_Name, whereClause, get_TrxName())
.setParameters(get_ID())
.scroll();
while(rs.hasNext()) {
rs.next().deleteEx(true);
}
}
finally {
rs.close();
DB.close(rs);
rs = null;
}
}
return true;

View File

@ -177,14 +177,15 @@ public class MSequence extends X_AD_Sequence
// get ID from http site
retValue = getNextOfficialID_HTTP(TableName);
if (retValue > 0) {
PreparedStatement updateSQL;
updateSQL = conn.prepareStatement("UPDATE AD_Sequence SET CurrentNextSys = ? + 1 WHERE AD_Sequence_ID = ?");
PreparedStatement updateSQL = null;
try {
updateSQL = conn.prepareStatement("UPDATE AD_Sequence SET CurrentNextSys = ? + 1 WHERE AD_Sequence_ID = ?");
updateSQL.setInt(1, retValue);
updateSQL.setInt(2, AD_Sequence_ID);
updateSQL.executeUpdate();
} finally {
updateSQL.close();
DB.close(updateSQL);
updateSQL = null;
}
}
gotFromHTTP = true;
@ -206,14 +207,15 @@ public class MSequence extends X_AD_Sequence
// get ID from http site
retValue = getNextProjectID_HTTP(TableName);
if (retValue > 0) {
PreparedStatement updateSQL;
updateSQL = conn.prepareStatement("UPDATE AD_Sequence SET CurrentNext = GREATEST(CurrentNext, ? + 1) WHERE AD_Sequence_ID = ?");
PreparedStatement updateSQL = null;
try {
updateSQL = conn.prepareStatement("UPDATE AD_Sequence SET CurrentNext = GREATEST(CurrentNext, ? + 1) WHERE AD_Sequence_ID = ?");
updateSQL.setInt(1, retValue);
updateSQL.setInt(2, AD_Sequence_ID);
updateSQL.executeUpdate();
} finally {
updateSQL.close();
DB.close(updateSQL);
updateSQL = null;
}
}
gotFromHTTP = true;
@ -222,23 +224,25 @@ public class MSequence extends X_AD_Sequence
}
if (! gotFromHTTP) {
PreparedStatement updateSQL;
int incrementNo = rs.getInt(3);
if (adempiereSys) {
updateSQL = conn
.prepareStatement("UPDATE AD_Sequence SET CurrentNextSys = CurrentNextSys + ? WHERE AD_Sequence_ID = ?");
retValue = rs.getInt(2);
} else {
updateSQL = conn
.prepareStatement("UPDATE AD_Sequence SET CurrentNext = CurrentNext + ? WHERE AD_Sequence_ID = ?");
retValue = rs.getInt(1);
}
try {
PreparedStatement updateSQL = null;
try
{
int incrementNo = rs.getInt(3);
if (adempiereSys) {
updateSQL = conn
.prepareStatement("UPDATE AD_Sequence SET CurrentNextSys = CurrentNextSys + ? WHERE AD_Sequence_ID = ?");
retValue = rs.getInt(2);
} else {
updateSQL = conn
.prepareStatement("UPDATE AD_Sequence SET CurrentNext = CurrentNext + ? WHERE AD_Sequence_ID = ?");
retValue = rs.getInt(1);
}
updateSQL.setInt(1, incrementNo);
updateSQL.setInt(2, AD_Sequence_ID);
updateSQL.executeUpdate();
} finally {
updateSQL.close();
DB.close(updateSQL);
updateSQL = null;
}
}
@ -263,14 +267,10 @@ public class MSequence extends X_AD_Sequence
finally
{
DB.close(rs, pstmt);
pstmt = null;
rs = null;
if (timeoutStatement != null){
try {
timeoutStatement.close();
}catch(Exception e){}
timeoutStatement = null;
}
pstmt = null;rs = null;
DB.close(timeoutStatement);
timeoutStatement = null;
if (conn != null)
{
try {
@ -478,6 +478,7 @@ public class MSequence extends X_AD_Sequence
finally
{
DB.close(updateSQL);
updateSQL = null;
}
}
else
@ -516,13 +517,12 @@ public class MSequence extends X_AD_Sequence
finally
{
DB.close(rs, pstmt);
pstmt = null;rs = null;
DB.close(timeoutStatement);
timeoutStatement = null;
// Finish
try
{
if (timeoutStatement != null) {
timeoutStatement.close();
timeoutStatement = null;
}
if (trx == null && conn != null) {
conn.close();
conn = null;

View File

@ -85,18 +85,23 @@ public class FinReportJasper extends FinReport
if (proc.getProcedureName() != null && proc.getProcedureName().length() > 0) {
// execute on this thread/connection
String sql = "{call " + proc.getProcedureName() + "(?)}";
CallableStatement cstmt = null;
try
{
CallableStatement cstmt = DB.prepareCall(sql); // ro??
cstmt = DB.prepareCall(sql); // ro??
cstmt.setInt(1, getAD_PInstance_ID());
cstmt.executeUpdate();
cstmt.close();
}
catch (Exception e)
{
log.log(Level.SEVERE, sql, e);
poInfo.setSummary (Msg.getMsg(Env.getCtx(), "ProcessRunError") + " " + e.getLocalizedMessage());
}
finally
{
DB.close(cstmt);
cstmt = null;
}
}
// TODO - allow java class preprocess if the classname <> ProcessUtil.JASPER_STARTER_CLASS

View File

@ -1056,6 +1056,7 @@ public final class DB
}
// Always close cursor
close(cs);
cs = null;
}
return no;
} // executeUpdate
@ -1121,9 +1122,8 @@ public final class DB
}
finally
{
if (rs != null)
rs.getStatement().close();
DB.close(rs);
close(rs.getStatement());
close(rs);rs = null;
}
}
timeoutStatement = conn.createStatement();
@ -1134,12 +1134,8 @@ public final class DB
}
} catch (SQLException e) {}
finally{
if (timeoutStatement != null) {
try {
timeoutStatement.close();
} catch (Exception e) {}
timeoutStatement = null;
}
DB.close(timeoutStatement);
timeoutStatement = null;
}
}
else
@ -1193,15 +1189,12 @@ public final class DB
} catch (SQLException e) {
}
finally{
if (timeoutStatement != null) {
try {
timeoutStatement.close();
} catch (Exception e) {}
close(timeoutStatement);
timeoutStatement = null;
}
}
}
DB.close(cs);
close(cs);
cs = null;
}
return no;
}

View File

@ -4,6 +4,7 @@ import org.compiere.model.MAssetAddition;
import org.compiere.model.POResultSet;
import org.compiere.model.Query;
import org.compiere.process.SvrProcess;
import org.compiere.util.DB;
/**
@ -21,11 +22,11 @@ public class A_Asset_Addition_ProcessAll extends SvrProcess
//
String whereClause = "AD_Client_ID=? AND IsActive=?"
+" AND "+MAssetAddition.COLUMNNAME_Processed+"=?";
POResultSet<MAssetAddition>
rs = new Query(getCtx(), MAssetAddition.Table_Name, whereClause, get_TrxName())
.setParameters(new Object[]{getAD_Client_ID(), "N", "N"})
.scroll();
POResultSet<MAssetAddition> rs = null;
try {
rs = new Query(getCtx(), MAssetAddition.Table_Name, whereClause, get_TrxName())
.setParameters(new Object[]{getAD_Client_ID(), "N", "N"})
.scroll();
while (rs.hasNext()) {
MAssetAddition a = rs.next();
boolean ret = a.processIt(MAssetAddition.DOCACTION_Complete);
@ -36,7 +37,7 @@ public class A_Asset_Addition_ProcessAll extends SvrProcess
}
}
finally {
rs.close(); rs = null;
DB.close(rs); rs = null;
}
//
return "OK/Error: "+cnt_ok+"/"+cnt_err;

View File

@ -78,8 +78,8 @@ public class SQLStatementElementHandler extends AbstractElementHandler {
int n = stmt.executeUpdate (sql);
log.info("Executed SQL Statement for PostgreSQL: "+ getStringValue(element,"statement") + " ReturnValue="+n);
} finally {
if (stmt != null)
stmt.close();
DB.close(stmt);
stmt = null;
}
}
@ -105,6 +105,7 @@ public class SQLStatementElementHandler extends AbstractElementHandler {
logImportDetail (ctx, impDetail, 0, "SQLStatement",1,"Execute");
} finally {
DB.close(pstmt);
pstmt = null;
}
}

View File

@ -232,26 +232,21 @@ public class VSQLProcess extends CPanel
log.log(Level.SEVERE, "process statement: " + sql + " - " + e.toString());
result.append("===> ").append(e.toString());
}
finally
{
DB.close(stmt);
stmt = null;
try
{
conn.close();
}
catch (SQLException e2)
{
log.log(Level.SEVERE, "processStatement - close connection", e2);
}
conn = null;
}
// Clean up
try
{
stmt.close();
}
catch (SQLException e1)
{
log.log(Level.SEVERE, "processStatement - close statement", e1);
}
stmt = null;
try
{
conn.close();
}
catch (SQLException e2)
{
log.log(Level.SEVERE, "processStatement - close connection", e2);
}
conn = null;
//
result.append(Env.NL);
return result.toString();

View File

@ -779,14 +779,7 @@ public final class Find extends CDialog
public void dispose()
{
log.config("");
// Find SQL
if (m_pstmt != null)
{
try {
m_pstmt.close();
} catch (SQLException e) {}
}
DB.close(m_pstmt);
m_pstmt = null;
// Remove action listener from custom fields - teo_sarca [ 1709292 ]

View File

@ -968,17 +968,22 @@ public final class VAccountDialog extends CDialog
sql.append("'").append(f_Alias.getValue()).append("'");
sql.append(" WHERE C_ValidCombination_ID=").append(IDvalue);
int i = 0;
PreparedStatement stmt = null;
try
{
java.sql.PreparedStatement stmt = DB.prepareStatement(sql.toString(),
stmt = DB.prepareStatement(sql.toString(),
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE, null);
i = stmt.executeUpdate();
stmt.close();
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql.toString(), e);
}
finally
{
DB.close(stmt);
stmt = null;
}
if (i == 0)
ADialog.error(m_WindowNo, this, "AccountNotUpdated");
}

View File

@ -188,8 +188,8 @@ public class VAssignment extends JComponent
{
try
{
if (m_pstmt != null)
m_pstmt.close();
DB.close(m_pstmt);
m_pstmt = null;
}
catch (Exception e)
{

View File

@ -238,26 +238,20 @@ public class WSQLProcess extends ADForm implements EventListener<Event>
log.log(Level.SEVERE, "process statement: " + sql + " - " + exception);
result.append("===> ").append(exception);
}
// Clean up
try
finally
{
stmt.close();
}
catch (SQLException e1)
{
log.log(Level.SEVERE, "processStatement - close statement", e1);
}
stmt = null;
try
{
conn.close();
}
catch (SQLException e2)
{
log.log(Level.SEVERE, "processStatement - close connection", e2);
}
conn = null;
DB.close(stmt);
stmt = null;
try
{
conn.close();
}
catch (SQLException e2)
{
log.log(Level.SEVERE, "processStatement - close connection", e2);
}
conn = null;
}
//
result.append(Env.NL);
return result.toString();

View File

@ -1789,15 +1789,9 @@ public class FindWindow extends Window implements EventListener<Event>, ValueCha
log.config("");
// Find SQL
if (m_pstmt != null)
{
try {
m_pstmt.close();
} catch (SQLException e) {}
}
DB.close(m_pstmt);
m_pstmt = null;
// TargetFields
if (m_targetFields != null)
m_targetFields.clear();

View File

@ -956,17 +956,22 @@ public final class WAccountDialog extends Window
sql.append("'").append(f_Alias.getValue()).append("'");
sql.append(" WHERE C_ValidCombination_ID=").append(IDvalue);
int i = 0;
PreparedStatement stmt = null;
try
{
java.sql.PreparedStatement stmt = DB.prepareStatement(sql.toString(),
stmt = DB.prepareStatement(sql.toString(),
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE, null);
i = stmt.executeUpdate();
stmt.close();
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql.toString(), e);
}
finally
{
DB.close(stmt);
stmt = null;
}
if (i == 0)
FDialog.error(m_WindowNo, this, "AccountNotUpdated");
}

View File

@ -372,17 +372,23 @@ public class Match
m_sql.toString(), "hdr", MRole.SQL_FULLYQUALIFIED, MRole.SQL_RO)
+ m_groupBy;
log.finest(sql);
Statement stmt = null;
ResultSet rs = null;
try
{
Statement stmt = DB.createStatement();
ResultSet rs = stmt.executeQuery(sql);
stmt = DB.createStatement();
rs = stmt.executeQuery(sql);
table.loadTable(rs);
stmt.close();
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql, e);
}
finally
{
DB.close(rs,stmt);
rs = null;stmt = null;
}
} // tableLoad
/**

View File

@ -831,10 +831,11 @@ public class DB_PostgreSQL implements AdempiereDatabase
public static void dumpLocks(Connection conn)
{
Statement stmt = null;
ResultSet rs = null;
try {
String sql = "select pg_class.relname,pg_locks.* from pg_class,pg_locks where pg_class.relfilenode=pg_locks.relation order by 1";
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
rs = stmt.executeQuery(sql);
int cnt = rs.getMetaData().getColumnCount();
System.out.println();
while (rs.next())
@ -852,10 +853,8 @@ public class DB_PostgreSQL implements AdempiereDatabase
} catch (Exception e) {
} finally {
try{
if (stmt != null)
stmt.close();
} catch (Exception e) {}
DB.close(rs,stmt);
rs = null;stmt = null;
}
}
@ -964,13 +963,24 @@ public class DB_PostgreSQL implements AdempiereDatabase
finally
{
if (rs != null)
rs.getStatement().close();
DB.close(rs.getStatement());
DB.close(rs);
rs = null;
}
Statement timeoutStatement = null;
try
{
timeoutStatement = conn.createStatement();
String sql = "SET " + (autoCommit ? "SESSION" : "LOCAL") + " statement_timeout TO " + ( timeOut > 0 ? Integer.toString(timeOut * 1000) : " DEFAULT ");
timeoutStatement.execute(sql);
}
finally
{
DB.close(timeoutStatement);
timeoutStatement = null;
}
Statement timeoutStatement = conn.createStatement();
String sql = "SET " + (autoCommit ? "SESSION" : "LOCAL") + " statement_timeout TO " + ( timeOut > 0 ? Integer.toString(timeOut * 1000) : " DEFAULT ");
timeoutStatement.execute(sql);
if (log.isLoggable(Level.FINEST))
{
log.finest("Set statement timeout to " + timeOut);
@ -1035,6 +1045,7 @@ public class DB_PostgreSQL implements AdempiereDatabase
if(stmt!=null)setStatementTimeout(stmt.getConnection(), currentTimeout);
} catch (SQLException e) {}
DB.close(rs, stmt);
rs = null;stmt = null;
}
}
return false;

View File

@ -228,14 +228,9 @@ public class DBDataSource extends compiereDataSource
*/
public void close()
{
try
{
if(m_resultSet != null)
m_resultSet.close();
if(m_pstmt != null)
m_pstmt.close();
}
catch (Exception e){}
DB.close(m_resultSet);
m_resultSet = null;
DB.close(m_pstmt);
m_pstmt = null;
}
}