BF [ 1885496 ] Performance NEEDS

- added caching support for MSysConfig
- added MSysConfigTest test case
This commit is contained in:
teo_sarca 2008-02-13 17:03:15 +00:00
parent ba4af07e4b
commit b37c0f827d
3 changed files with 131 additions and 66 deletions

View File

@ -13,11 +13,15 @@
*****************************************************************************/ *****************************************************************************/
package org.compiere.model; package org.compiere.model;
import java.sql.*; import java.sql.PreparedStatement;
import java.util.*; import java.sql.ResultSet;
import java.util.logging.*; import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import org.compiere.util.*; import org.compiere.util.CCache;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
/** /**
* System Configuration * System Configuration
@ -25,6 +29,8 @@ import org.compiere.util.*;
* @author Armen Rizal * @author Armen Rizal
* @version $Id: MSysConfig.java,v 1.5 2005/11/28 11:56:45 armen Exp $ * @version $Id: MSysConfig.java,v 1.5 2005/11/28 11:56:45 armen Exp $
* Contributor: Carlos Ruiz - globalqss - [ 1800371 ] System Configurator Enhancements * Contributor: Carlos Ruiz - globalqss - [ 1800371 ] System Configurator Enhancements
* @author Teo Sarca, SC ARHIPAC SERVICE SRL
* <li>BF [ 1885496 ] Performance NEEDS
*/ */
public class MSysConfig extends X_AD_SysConfig public class MSysConfig extends X_AD_SysConfig
{ {
@ -55,6 +61,8 @@ public class MSysConfig extends X_AD_SysConfig
/** Static Logger */ /** Static Logger */
private static CLogger s_log = CLogger.getCLogger (MSysConfig.class); private static CLogger s_log = CLogger.getCLogger (MSysConfig.class);
/** Cache */
private static CCache<String, String> s_cache = new CCache<String, String>(Table_Name, 40, 0);
/** /**
* Get system configuration property of type string * Get system configuration property of type string
@ -64,25 +72,7 @@ public class MSysConfig extends X_AD_SysConfig
*/ */
public static String getValue(String Name, String defaultValue) public static String getValue(String Name, String defaultValue)
{ {
String str = null; return getValue(Name, defaultValue, 0, 0);
String sql = "SELECT Value FROM AD_SysConfig WHERE Name=? AND AD_Client_ID = 0 AND AD_Org_ID = 0 AND IsActive='Y'";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql, null);
pstmt.setString(1, Name);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
str = rs.getString(1);
rs.close();
pstmt.close();
}
catch (SQLException e)
{
s_log.log(Level.SEVERE, "getValue", e);
}
if (str == null)
return defaultValue;
return (str.trim());
} }
/** /**
@ -92,7 +82,7 @@ public class MSysConfig extends X_AD_SysConfig
*/ */
public static String getValue(String Name) public static String getValue(String Name)
{ {
return (getValue(Name, null)); return getValue(Name, null);
} }
/** /**
@ -173,28 +163,7 @@ public class MSysConfig extends X_AD_SysConfig
*/ */
public static String getValue(String Name, String defaultValue, int AD_Client_ID) public static String getValue(String Name, String defaultValue, int AD_Client_ID)
{ {
String str = null; return getValue(Name, defaultValue, AD_Client_ID, 0);
String sql = "SELECT Value FROM AD_SysConfig WHERE Name=? AND AD_Client_ID = ? AND AD_Org_ID = 0 AND IsActive='Y'";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql, null);
pstmt.setString(1, Name);
pstmt.setInt(2, AD_Client_ID);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
str = rs.getString(1);
rs.close();
pstmt.close();
}
catch (SQLException e)
{
s_log.log(Level.SEVERE, "getValue", e);
}
if (str == null) {
// if not found by client - get the system parameter
return getValue(Name, defaultValue);
}
return (str.trim());
} }
/** /**
@ -290,29 +259,44 @@ public class MSysConfig extends X_AD_SysConfig
*/ */
public static String getValue(String Name, String defaultValue, int AD_Client_ID, int AD_Org_ID) public static String getValue(String Name, String defaultValue, int AD_Client_ID, int AD_Org_ID)
{ {
String str = null; String key = ""+AD_Client_ID+"_"+AD_Org_ID+"_"+Name;
String sql = "SELECT Value FROM AD_SysConfig WHERE Name=? AND AD_Client_ID = ? AND AD_Org_ID = ? AND IsActive='Y'"; String str = s_cache.get(key);
if (str != null)
return str;
//
String sql = "SELECT Value FROM AD_SysConfig"
+ " WHERE Name=? AND AD_Client_ID IN (0, ?) AND AD_Org_ID IN (0, ?) AND IsActive='Y'"
+ " ORDER BY AD_Client_ID DESC, AD_Org_ID DESC";
PreparedStatement pstmt = null;
ResultSet rs = null;
try try
{ {
PreparedStatement pstmt = DB.prepareStatement(sql, null); pstmt = DB.prepareStatement(sql, null);
pstmt.setString(1, Name); pstmt.setString(1, Name);
pstmt.setInt(2, AD_Client_ID); pstmt.setInt(2, AD_Client_ID);
pstmt.setInt(3, AD_Org_ID); pstmt.setInt(3, AD_Org_ID);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
if (rs.next()) if (rs.next())
str = rs.getString(1); str = rs.getString(1);
rs.close();
pstmt.close();
} }
catch (SQLException e) catch (SQLException e)
{ {
s_log.log(Level.SEVERE, "getValue", e); s_log.log(Level.SEVERE, "getValue", e);
} }
if (str == null) { finally
// if not found by organization - get the client parameter {
return getValue(Name, defaultValue, AD_Client_ID); DB.close(rs, pstmt);
rs = null; pstmt = null;
}
//
if (str != null) {
str = str.trim();
s_cache.put(key, str);
return str;
}
else {
return defaultValue;
} }
return (str.trim());
} }
/** /**
@ -324,7 +308,7 @@ public class MSysConfig extends X_AD_SysConfig
*/ */
public static String getValue(String Name, int AD_Client_ID, int AD_Org_ID) public static String getValue(String Name, int AD_Client_ID, int AD_Org_ID)
{ {
return (getValue(Name, null, AD_Client_ID, AD_Org_ID)); return getValue(Name, null, AD_Client_ID, AD_Org_ID);
} }
/** /**
@ -416,20 +400,25 @@ public class MSysConfig extends X_AD_SysConfig
// Get the configuration level from the System Record // Get the configuration level from the System Record
String configLevel = null; String configLevel = null;
String sql = "SELECT ConfigurationLevel FROM AD_SysConfig WHERE Name=? AND AD_Client_ID = 0 AND AD_Org_ID = 0"; String sql = "SELECT ConfigurationLevel FROM AD_SysConfig WHERE Name=? AND AD_Client_ID = 0 AND AD_Org_ID = 0";
PreparedStatement pstmt = null;
ResultSet rs = null;
try try
{ {
PreparedStatement pstmt = DB.prepareStatement(sql, null); pstmt = DB.prepareStatement(sql, null);
pstmt.setString(1, getName()); pstmt.setString(1, getName());
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
if (rs.next()) if (rs.next())
configLevel = rs.getString(1); configLevel = rs.getString(1);
rs.close();
pstmt.close();
} }
catch (SQLException e) catch (SQLException e)
{ {
s_log.log(Level.SEVERE, "getValue", e); s_log.log(Level.SEVERE, "getValue", e);
} }
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
if (configLevel == null) { if (configLevel == null) {
// not found for system // not found for system
@ -439,19 +428,22 @@ public class MSysConfig extends X_AD_SysConfig
sql = "SELECT ConfigurationLevel FROM AD_SysConfig WHERE Name=? AND AD_Client_ID = ? AND AD_Org_ID = 0"; sql = "SELECT ConfigurationLevel FROM AD_SysConfig WHERE Name=? AND AD_Client_ID = ? AND AD_Org_ID = 0";
try try
{ {
PreparedStatement pstmt = DB.prepareStatement(sql, null); pstmt = DB.prepareStatement(sql, null);
pstmt.setString(1, getName()); pstmt.setString(1, getName());
pstmt.setInt(2, getAD_Client_ID()); pstmt.setInt(2, getAD_Client_ID());
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
if (rs.next()) if (rs.next())
configLevel = rs.getString(1); configLevel = rs.getString(1);
rs.close();
pstmt.close();
} }
catch (SQLException e) catch (SQLException e)
{ {
s_log.log(Level.SEVERE, "getValue", e); s_log.log(Level.SEVERE, "getValue", e);
} }
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
} }
} }

View File

@ -16,6 +16,7 @@ public class FunctionalTestSuite {
suite.addTestSuite(MLocationTest.class); suite.addTestSuite(MLocationTest.class);
suite.addTestSuite(POTest.class); suite.addTestSuite(POTest.class);
suite.addTestSuite(MStorageTest.class); suite.addTestSuite(MStorageTest.class);
suite.addTestSuite(MSysConfigTest.class);
//$JUnit-END$ //$JUnit-END$
return suite; return suite;
} }

View File

@ -0,0 +1,72 @@
/**
*
*/
package test.functional;
import java.util.Properties;
import org.compiere.model.MSysConfig;
import org.compiere.util.DB;
import test.AdempiereTestCase;
/**
* MSysConfig Test Case
* @author Teo Sarca, SC ARHIPAC SERVICE SRL
*
*/
public class MSysConfigTest extends AdempiereTestCase {
private String varname = null;
@Override
protected void setUp() throws Exception {
super.setUp();
//
varname = "MSysConfigTestVariable" + System.currentTimeMillis();
new SysConfigTest(getCtx(), 0, 0, varname, "0_0", null).save();
new SysConfigTest(getCtx(), 11, 0, varname, "11_0", null).save();
new SysConfigTest(getCtx(), 11, 11, varname, "11_11", null).save();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
//
String sql = "DELETE FROM "+MSysConfig.Table_Name
+" WHERE "+MSysConfig.COLUMNNAME_Name+"=?";
DB.executeUpdate(sql, new Object[]{varname}, false, null);
}
public void testGetSet1() throws Exception {
//
assertEquals("0_0", MSysConfig.getValue(varname));
//
assertEquals("0_0", MSysConfig.getValue(varname, 0));
assertEquals("11_0", MSysConfig.getValue(varname, 11));
assertEquals("0_0", MSysConfig.getValue(varname, 12345));
//
assertEquals("0_0", MSysConfig.getValue(varname, 0, 0));
assertEquals("11_0", MSysConfig.getValue(varname, 11, 0));
assertEquals("11_11", MSysConfig.getValue(varname, 11, 11));
assertEquals("0_0", MSysConfig.getValue(varname, 12345, 12345));
assertEquals("11_0", MSysConfig.getValue(varname, 11, 12345));
}
private static class SysConfigTest extends MSysConfig {
private static final long serialVersionUID = 1L;
public SysConfigTest(Properties ctx,
int AD_Client_ID, int AD_Org_ID,
String Name, String Value,
String trxName)
{
super(ctx, 0, trxName);
setAD_Client_ID(AD_Client_ID);
setAD_Org_ID(AD_Org_ID);
setName(Name);
setValue(Value);
setConfigurationLevel(CONFIGURATIONLEVEL_Organization);
}
};
}