MSystem: properly close ResultSets

* minor improvements
This commit is contained in:
teo_sarca 2008-11-13 14:09:37 +00:00
parent 295f01f2e8
commit 2d8e14b412
1 changed files with 61 additions and 106 deletions

View File

@ -16,12 +16,31 @@
*****************************************************************************/ *****************************************************************************/
package org.compiere.model; package org.compiere.model;
import java.lang.management.*; import java.lang.management.ManagementFactory;
import java.sql.*; import java.lang.management.MemoryMXBean;
import java.util.*; import java.lang.management.MemoryPoolMXBean;
import java.util.logging.*; import java.lang.management.RuntimeMXBean;
import org.compiere.db.*; import java.lang.management.ThreadMXBean;
import org.compiere.util.*; import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.exceptions.DBException;
import org.compiere.db.CConnection;
import org.compiere.db.Database;
import org.compiere.db.LDAP;
import org.compiere.util.CLogMgt;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.DisplayType;
import org.compiere.util.Env;
import org.compiere.util.Ini;
import org.compiere.util.TimeUtil;
/** /**
* System Record (just one) * System Record (just one)
@ -31,6 +50,8 @@ import org.compiere.util.*;
*/ */
public class MSystem extends X_AD_System public class MSystem extends X_AD_System
{ {
private static final long serialVersionUID = 1L;
/** /**
* Load System Record * Load System Record
* @param ctx context * @param ctx context
@ -170,31 +191,7 @@ public class MSystem extends X_AD_System
+ "||'L'|| (SELECT " + DB.TO_CHAR("COUNT(*)", DisplayType.Number, Env.getAD_Language(Env.getCtx())) + " FROM C_InvoiceLine)" + "||'L'|| (SELECT " + DB.TO_CHAR("COUNT(*)", DisplayType.Number, Env.getAD_Language(Env.getCtx())) + " FROM C_InvoiceLine)"
+ "||'M'|| (SELECT " + DB.TO_CHAR("COUNT(*)", DisplayType.Number, Env.getAD_Language(Env.getCtx())) + " FROM M_Transaction)" + "||'M'|| (SELECT " + DB.TO_CHAR("COUNT(*)", DisplayType.Number, Env.getAD_Language(Env.getCtx())) + " FROM M_Transaction)"
+ " FROM AD_System"; + " FROM AD_System";
PreparedStatement pstmt = null; s = DB.getSQLValueString(null, sql);
try
{
pstmt = DB.prepareStatement (sql, null);
ResultSet rs = pstmt.executeQuery ();
if (rs.next ())
s = rs.getString(1);
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;
}
} }
return s; return s;
} // getStatisticsInfo } // getStatisticsInfo
@ -209,33 +206,28 @@ public class MSystem extends X_AD_System
String s = super.getProfileInfo (); String s = super.getProfileInfo ();
if (s == null || recalc) if (s == null || recalc)
{ {
String sql = "SELECT Value FROM AD_Client " final String sql = "SELECT Value FROM AD_Client "
+ " WHERE IsActive='Y' ORDER BY AD_Client_ID DESC"; + " WHERE IsActive='Y' ORDER BY AD_Client_ID DESC";
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
try try
{ {
pstmt = DB.prepareStatement (sql, null); pstmt = DB.prepareStatement (sql, null);
ResultSet rs = pstmt.executeQuery (); rs = pstmt.executeQuery ();
while (rs.next ()) while (rs.next ())
{
sb.append(rs.getString(1)).append('|'); sb.append(rs.getString(1)).append('|');
rs.close ();
pstmt.close ();
pstmt = null;
} }
catch (Exception e)
{
log.log (Level.SEVERE, sql, e);
} }
try catch (SQLException e)
{ {
if (pstmt != null) throw new DBException(e, sql);
pstmt.close ();
pstmt = null;
} }
catch (Exception e) finally
{ {
pstmt = null; DB.close(rs, pstmt);
rs = null; pstmt = null;
} }
s = sb.toString(); s = sb.toString();
} }
@ -250,34 +242,31 @@ public class MSystem extends X_AD_System
protected boolean beforeSave (boolean newRecord) protected boolean beforeSave (boolean newRecord)
{ {
// Mandatory Values // Mandatory Values
if (get_Value("IsAutoErrorReport") == null) if (get_Value(COLUMNNAME_IsAutoErrorReport) == null)
setIsAutoErrorReport (true); setIsAutoErrorReport (true);
// //
boolean userChange = Ini.isClient() && boolean userChange = Ini.isClient() &&
(is_ValueChanged("Name") (is_ValueChanged(COLUMNNAME_Name)
|| is_ValueChanged("UserName") || is_ValueChanged(COLUMNNAME_UserName)
|| is_ValueChanged("Password") || is_ValueChanged(COLUMNNAME_Password)
|| is_ValueChanged("LDAPHost") || is_ValueChanged(COLUMNNAME_LDAPHost)
|| is_ValueChanged("LDAPDomain") || is_ValueChanged(COLUMNNAME_LDAPDomain)
|| is_ValueChanged("CustomPrefix") || is_ValueChanged(COLUMNNAME_CustomPrefix)
); );
if (userChange) if (userChange)
{ {
String name = getName(); String name = getName();
if (name.equals("?") || name.length() < 2) if (name.equals("?") || name.length() < 2)
{ {
log.saveError("Error", "Define a unique System name (e.g. Company name) not " + name); throw new AdempiereException("Define a unique System name (e.g. Company name) not " + name);
return false;
} }
if (getUserName().equals("?") || getUserName().length() < 2) if (getUserName().equals("?") || getUserName().length() < 2)
{ {
log.saveError("Error", "Use the same EMail address as in the Adempiere Web Store"); throw new AdempiereException("Use the same EMail address as in the Adempiere Web Store");
return false;
} }
if (getPassword().equals("?") || getPassword().length() < 2) if (getPassword().equals("?") || getPassword().length() < 2)
{ {
log.saveError("Error", "Use the same Password as in the Adempiere Web Store"); throw new AdempiereException("Use the same Password as in the Adempiere Web Store");
return false;
} }
} }
// //
@ -310,7 +299,7 @@ public class MSystem extends X_AD_System
/************************************************************************** /**************************************************************************
* Check valididity * Check validity
* @return true if valid * @return true if valid
*/ */
public boolean isValid() public boolean isValid()
@ -350,7 +339,6 @@ public class MSystem extends X_AD_System
*/ */
public boolean setInfo() public boolean setInfo()
{ {
// log.severe("setInfo");
if (!TimeUtil.getDay(getUpdated()).before(TimeUtil.getDay(null))) if (!TimeUtil.getDay(getUpdated()).before(TimeUtil.getDay(null)))
return false; return false;
try try
@ -377,39 +365,13 @@ public class MSystem extends X_AD_System
*/ */
private void setInternalUsers() private void setInternalUsers()
{ {
String sql = "SELECT COUNT(DISTINCT (u.AD_User_ID)) AS iu " final String sql = "SELECT COUNT(DISTINCT (u.AD_User_ID)) AS iu "
+ "FROM AD_User u" + "FROM AD_User u"
+ " INNER JOIN AD_User_Roles ur ON (u.AD_User_ID=ur.AD_User_ID) " + " INNER JOIN AD_User_Roles ur ON (u.AD_User_ID=ur.AD_User_ID) "
+ "WHERE u.AD_Client_ID<>11" // no Demo + "WHERE u.AD_Client_ID<>11" // no Demo
+ " AND u.AD_User_ID NOT IN (0,100)"; // no System/SuperUser + " AND u.AD_User_ID NOT IN (0,100)"; // no System/SuperUser
PreparedStatement pstmt = null; int internalUsers = DB.getSQLValue(null, sql);
try
{
pstmt = DB.prepareStatement (sql, null);
ResultSet rs = pstmt.executeQuery ();
if (rs.next ())
{
int internalUsers = rs.getInt (1);
setSupportUnits(internalUsers); setSupportUnits(internalUsers);
}
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;
}
} // setInternalUsers } // setInternalUsers
/** /**
@ -428,36 +390,29 @@ public class MSystem extends X_AD_System
// //
String dbName = null; String dbName = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = null; String sql = null;
try try
{ {
String dbType = CConnection.get().getDatabase().getName(); String dbType = CConnection.get().getDatabase().getName();
sql = getDBInfoSQL(dbType); sql = getDBInfoSQL(dbType);
pstmt = DB.prepareStatement (sql, null); pstmt = DB.prepareStatement (sql, null);
ResultSet rs = pstmt.executeQuery (); rs = pstmt.executeQuery ();
if (rs.next()) if (rs.next())
{ {
// dbAddress = rs.getString(1); // dbAddress = rs.getString(1);
dbName = rs.getString(2); dbName = rs.getString(2);
setDBInstance(dbName.toLowerCase()); setDBInstance(dbName.toLowerCase());
} }
rs.close ();
pstmt.close ();
pstmt = null;
} }
catch (Exception e) catch (SQLException e)
{ {
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;
} }
} // setDBInfo } // setDBInfo
@ -496,7 +451,7 @@ public class MSystem extends X_AD_System
if (CLogMgt.isLevelFiner()) if (CLogMgt.isLevelFiner())
{ {
List<MemoryPoolMXBean> list = ManagementFactory.getMemoryPoolMXBeans(); List<MemoryPoolMXBean> list = ManagementFactory.getMemoryPoolMXBeans();
Iterator it = list.iterator(); Iterator<MemoryPoolMXBean> it = list.iterator();
while (it.hasNext()) while (it.hasNext())
{ {
MemoryPoolMXBean pool = (MemoryPoolMXBean)it.next(); MemoryPoolMXBean pool = (MemoryPoolMXBean)it.next();