From 8a02f8532e8ae998212b58e40415816e15e28a36 Mon Sep 17 00:00:00 2001 From: mh Date: Sat, 9 Feb 2002 16:52:08 +0000 Subject: [PATCH] moved db pool initialization to MirConfig completely. Database.jave also gains better error handling. --- source/Mir.java | 11 +++---- source/OpenMir.java | 9 +++--- source/mir/misc/MirConfig.java | 43 +++++++++++++++++++++++---- source/mir/servlet/AbstractServlet.java | 15 ++++++++-- source/mir/storage/Database.java | 52 +++++++++++++++++++++------------ 5 files changed, 96 insertions(+), 34 deletions(-) diff --git a/source/Mir.java b/source/Mir.java index 1c0de3e4..a09d7cd6 100755 --- a/source/Mir.java +++ b/source/Mir.java @@ -4,12 +4,14 @@ import java.net.*; import java.lang.reflect.*; import javax.servlet.*; import javax.servlet.http.*; +import javax.servlet.UnavailableException; import java.sql.*; import freemarker.template.*; import mir.misc.*; import mir.servlet.*; +import mir.storage.StorageObjectException; import mircoders.servlet.*; import mircoders.module.*; @@ -29,17 +31,16 @@ public class Mir extends AbstractServlet private static ModuleUsers usersModule=null; private static ModuleMessage messageModule=null; - private static boolean confed=false; + //private static boolean confed=false; public HttpSession session; - public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doPost(req,res); } public void doPost(HttpServletRequest req, HttpServletResponse res) - throws ServletException, IOException { + throws ServletException, IOException, UnavailableException { long startTime = (new java.util.Date()).getTime(); Class theServletModule; @@ -48,8 +49,8 @@ public class Mir extends AbstractServlet String http=""; // get the configration - if (!confed){ - confed = getConfig(req); + if(getServletContext().getAttribute("mir.confed") == null) { + getConfig(req); } MirConfig.setServletName(getServletName()); diff --git a/source/OpenMir.java b/source/OpenMir.java index 2002b9f2..96820b2d 100755 --- a/source/OpenMir.java +++ b/source/OpenMir.java @@ -26,7 +26,7 @@ import mircoders.storage.*; public class OpenMir extends AbstractServlet { - private static boolean confed=false; + //private static boolean confed=false; private static String lang; public HttpSession session; @@ -42,10 +42,11 @@ public class OpenMir extends AbstractServlet { long sessionConnectTime=0; - if (!confed){ - confed = getConfig(req); - } + // get the configration + if(getServletContext().getAttribute("mir.confed") == null) { + getConfig(req); + } session = req.getSession(); if(session.getAttribute("Language")==null){ diff --git a/source/mir/misc/MirConfig.java b/source/mir/misc/MirConfig.java index 4adcd34c..162d43e4 100755 --- a/source/mir/misc/MirConfig.java +++ b/source/mir/misc/MirConfig.java @@ -6,6 +6,7 @@ import java.io.*; import java.util.*; import java.lang.*; import mir.storage.StorageObjectException; +import mir.storage.DatabaseAdaptor; import com.codestudio.util.*; /** @@ -27,7 +28,7 @@ import com.codestudio.util.*; public class MirConfig extends Configuration { - private static HashMap configHash = new HashMap(); + private static HashMap configHash = null; private static HashMap brokerHash = new HashMap(); private static int instances=0; @@ -39,9 +40,10 @@ public class MirConfig extends Configuration { * @param name, The name of the servlet (usually "Mir") * @param confName, the name of the config file to load. */ - public static void initConfig(String home, String uri, String name, String confName) { + public static synchronized void initConfig(String home, String uri, + String name, String confName) { initConfResource(confName); - + configHash = new HashMap(); configHash.put("Home", home); configHash.put("RootUri", uri); @@ -80,8 +82,37 @@ public class MirConfig extends Configuration { (String)configHash.get(propName); } - public static void addBroker(String driver, String URL) throws StorageObjectException { + public static void initDbPool () throws StorageObjectException { + if (configHash == null) { + throw new StorageObjectException("MirConfig -- Trying initialize "+ + "DB pool when system not yet "+ + "configured"); + } + String dbUser=getProp("Database.Username"); + String dbPassword=getProp("Database.Password"); + String dbHost=getProp("Database.Host"); + String dbAdapName=getProp("Database.Adaptor"); + DatabaseAdaptor adaptor; + try { + adaptor = (DatabaseAdaptor)Class.forName(dbAdapName).newInstance(); + } catch (Exception e) { + throw new StorageObjectException("Could not load DB adapator: "+ + e.toString()); + } + String dbDriver=adaptor.getDriver(); + String dbUrl=adaptor.getURL(dbUser,dbPassword, dbHost); + System.out.println("adding Broker with: " +dbDriver+":"+dbUrl ); + addBroker( dbDriver, dbUrl); + } + + public static void addBroker(String driver, String URL) + throws StorageObjectException { + if (configHash == null) { + throw new StorageObjectException("MirConfig -- Trying initialize "+ + "DB pool when system not yet "+ + "configured"); + } String username,passwd,min,max,log,reset,dbname,dblogfile; if(!brokerHash.containsKey("Pool.broker")){ @@ -109,7 +140,9 @@ public class MirConfig extends Configuration { meta.setMaximumSize(Integer.parseInt(max)); meta.setMinimumSize(Integer.parseInt(min)); meta.setCacheEnabled(true); - //meta.setDebugging(true); + meta.setCacheEnabled(true); + meta.setCacheSize(15); + meta.setDebugging(true); meta.setLogFile(dblogfile+".pool"); JDBCPool pool = SQLManager.getInstance().createPool(meta); diff --git a/source/mir/servlet/AbstractServlet.java b/source/mir/servlet/AbstractServlet.java index 53e75c62..9b3c400a 100755 --- a/source/mir/servlet/AbstractServlet.java +++ b/source/mir/servlet/AbstractServlet.java @@ -3,6 +3,7 @@ package mir.servlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; +import javax.servlet.UnavailableException; import java.io.File; import java.util.Locale; import java.util.HashMap; @@ -11,6 +12,7 @@ import mir.misc.HTMLTemplateProcessor; import mir.misc.StringUtil; import mir.misc.MirConfig; import mir.misc.Logfile; +import mir.storage.StorageObjectException; /** * Title: Mir @@ -28,7 +30,8 @@ public abstract class AbstractServlet extends HttpServlet { /** * the configration */ - protected boolean getConfig(HttpServletRequest req) { + protected boolean getConfig(HttpServletRequest req) + throws UnavailableException { String RealPath = super.getServletContext().getRealPath("/"); String Uri = req.getRequestURI(); @@ -38,11 +41,19 @@ public abstract class AbstractServlet extends HttpServlet { // init config MirConfig.initConfig(RealPath, RootUri, Name, getInitParameter("Config")); - theLog = Logfile.getInstance(this.getClass().getName()); + theLog = Logfile.getInstance(MirConfig.getPropWithHome(Name+".Logfile")); theLog.printInfo(Name + " started."); theLog.printInfo("Path is: " + MirConfig.getProp("Home")); theLog.printInfo("Root URI is: " + MirConfig.getProp("RootUri")); theLog.printInfo("Lang is: " + MirConfig.getProp("StandardLanguage")); + try { + MirConfig.initDbPool(); + } catch ( StorageObjectException e) { + throw new UnavailableException( + "Could not initialize database pool. -- " + + e.toString(), 0); + } + super.getServletContext().setAttribute("mir.confed", new Boolean(true)); return true; } diff --git a/source/mir/storage/Database.java b/source/mir/storage/Database.java index e46db2e8..b8d5f59c 100755 --- a/source/mir/storage/Database.java +++ b/source/mir/storage/Database.java @@ -33,7 +33,8 @@ public class Database implements StorageObject { protected String thePKeyName="id"; protected int thePKeyType; protected boolean evaluatedMetaData=false; - protected ArrayList metadataFields,metadataLabels,metadataNotNullFields; + protected ArrayList metadataFields,metadataLabels, + metadataNotNullFields; protected int[] metadataTypes; protected Class theEntityClass; protected StorageObject myselfDatabase; @@ -59,25 +60,31 @@ public class Database implements StorageObject { * @param String confFilename Dateiname der Konfigurationsdatei */ public Database() throws StorageObjectException { - theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("Database.Logfile")); - String database_username=MirConfig.getProp("Database.Username"); - String database_password=MirConfig.getProp("Database.Password"); - String database_host=MirConfig.getProp("Database.Host"); + theLog = Logfile.getInstance(MirConfig.getProp("Home")+ + MirConfig.getProp("Database.Logfile")); String theAdaptorName=MirConfig.getProp("Database.Adaptor"); + defaultLimit = Integer.parseInt(MirConfig.getProp("Database.Limit")); try { theEntityClass = Class.forName("mir.entity.GenericEntity"); theAdaptor = (DatabaseAdaptor)Class.forName(theAdaptorName).newInstance(); - defaultLimit = Integer.parseInt(MirConfig.getProp("Database.Limit")); + } catch (Exception e){ + theLog.printError("Error in Database() constructor with "+ + theAdaptorName + " -- " +e.toString()); + throw new StorageObjectException("Error in Database() constructor with " + +e.toString()); + } + /*String database_username=MirConfig.getProp("Database.Username"); + String database_password=MirConfig.getProp("Database.Password"); + String database_host=MirConfig.getProp("Database.Host"); + try { database_driver=theAdaptor.getDriver(); - database_url=theAdaptor.getURL(database_username,database_password,database_host); - theLog.printDebugInfo("adding Broker with: " +database_driver+":"+database_url ); + database_url=theAdaptor.getURL(database_username,database_password, + database_host); + theLog.printDebugInfo("adding Broker with: " +database_driver+":"+ + database_url ); MirConfig.addBroker(database_driver,database_url); //myBroker=MirConfig.getBroker(); - } - catch (Exception e){ - theLog.printError("Bei Konstruktion von Database() with " + theAdaptorName + " -- " +e.toString()); - throw new StorageObjectException(e.toString()); - } + }*/ } /** @@ -121,7 +128,8 @@ public class Database implements StorageObject { /* * Dient dazu vererbte Tabellen bei objectrelationalen DBMS - * zu speichern, wenn die id einer Tabelle in der parenttabelle verwaltet wird. + * zu speichern, wenn die id einer Tabelle in der parenttabelle verwaltet + * wird. * @return liefert theCoreTabel als String zurueck, wenn gesetzt, sonst * the Table */ @@ -808,6 +816,10 @@ public class Database implements StorageObject { // execute sql try { con = getPooledCon(); + } catch (Exception e) { + throw new StorageObjectException(e.toString()); + } + try { stmt = con.createStatement(); ResultSet rs = executeSql(stmt, sql.toString()); if (rs != null) { @@ -830,8 +842,8 @@ public class Database implements StorageObject { rs.close(); } } catch (Exception e) { - theLog.printDebugInfo(e.toString()); - throw new StorageObjectException(e.toString()); + theLog.printError("getPopupData: "+e.toString()); + throw new StorageObjectException(e.toString()); } finally { freeConnection(con, stmt); } @@ -1042,7 +1054,8 @@ public class Database implements StorageObject { public Connection getPooledCon() throws StorageObjectException { - /*try{ + /* @todo , doublecheck but I'm pretty sure that this is unnecessary. -mh + try{ Class.forName("com.codestudio.sql.PoolMan").newInstance(); } catch (Exception e){ throw new StorageObjectException("Could not find the PoolMan Driver" @@ -1052,7 +1065,10 @@ public class Database implements StorageObject { try{ con = SQLManager.getInstance().requestConnection(); } catch(SQLException e){ - throw new StorageObjectException("No connection to the database"); + theLog.printError("could not connect to the database "+e.toString()); + System.err.println("could not connect to the database "+e.toString()); + throw new StorageObjectException("Could not connect to the database"+ + e.toString()); } return con; } -- 2.11.0