//*********************email 0****************************************
Marc Krisjanous wrote:

> [...]
> I have a class that manages a log file which all objects within the system
> can write messages to.  The class utilises the Singleton Instance design
> pattern so only one instance is accessible.  The main method that writes the
> message to the log file is synchronized.  Here is the skeleton class:
>
> public class EventLogController {
>
>         public static EventLogController getInstance()
>         {
>         //Singleton implementation
>         }
>
>         public synchronized boolean writeToEventLog(String fileName, String
> message)
>         {
>         //write to file
>         }
>
> }//end class
>
> Question:
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Should I still use this approach or should I create the writeToEventLog as
> static thus removing the need to kept an instance of the class.
>
> Best Regards
>
> Marc
> [...]

//*********************email 1****************************************
Make sure, that getInstance() is synchronized or it could
break that design pattern, depending on the implementation.

Greetings
Stefan


//*********************email 2****************************************
Hello,
from my understanding of what you said I will need to wrap every call to the
EventLogController.getInstance() method every time an object calls it?

Would that mean that I then do not need to have the writeToEventLog method
synchronized??

Best Regards

Marc





Hi :-)   I also think you need to make your getInstance() synchronized
if you want to use SingleTon.  and the following is just my guessing:


0
IMO,  if we want to manage a *log file/DBConeection pool/or other* with Servlet,

perhaps we need the following three things:
     *    Controller/Holder, for example -> EventLogController or
           DBConnectionPoolHolder
     *     locker which is used for synchronized some code
     *    Servlet class

I guess the most important things are the following two things:
*   we need to make our code thread-safe, SO we need to find something
     which must be *only one As Sure As Possible* in our Sevlet container,
     and we can use it to lock our code.
*   perhaps we need to reduce  the loading/unloading times of our
    Controller/Holder, for example, we need to avoid opening/closing/
    reopening/reclosing our DBConnection many times.

SingleTon is just one way to do the above two things, I guess we also can do
it by other ways.


1
     #  Controller/Holder
           normally it is a class, it could be a SingleTon, but it also could
not be
           a SingleTon.
     #  locker
           it could be:
          [a]   *  a static field in Servlet class
                 *  if we only have one Servlet definition, it also could be a
instance field
                     in Servlet class
                 *  Servlet class itself
                 *  if we only have one Servlet definition, it also could be the
instance of
                    Servlet class -> *this* in Servlet class
           [b]  *  a static field in another class ->locker class
                 *  a instance field in another class ->locker class, if now
locker class
                     is a SingleTon, or we use other way to make sure we only
have only one
                     instance of locker class.
                 *  locker class itself
                 *  the only one instance of another class ->locker class, if
now locker class
                     is a SingleTon, or we use other way to make sure we only
have only one
                     instance of locker class.
           [c]  *  a static field in Controller/Holder class
                 *  a instance field in Controller/Holder class,  if now
Controller/Holder class
                     is a SingleTon, or we use other way to make sure we only
have only one
                     instance of Controller/Holder class.
                 *  Controller/Holder class itself
                 *  the only one instance of Controller/Holder class,  if now
Controller/Holder
                     is a SingleTon, or we use other way to make sure we only
have only one
                     instance of Controller/Holder class.

2    for example, I wrote a simple DBConnection pool, if it is wrong, please
correct it
       thanks in advance :-)


   [a]    ********************  the simple DBConnection pool
*********************

public class outsideDBCPHoder implements java.io.Serializable {
private static outsideDBCPHoder onlyOneInstance;
private java.util.Vector pool=new java.util.Vector();

private outsideDBCPHoder() {}


   public static synchronized outsideDBCPHoder getOnlyOneInstance() {
      if (onlyOneInstance == null) {
      onlyOneInstance = new outsideDBCPHoder();
      }
   return onlyOneInstance;
   }


   public synchronized java.sql.Connection getDBConnection() {
      java.sql.Connection con = null;
      if ( pool.isEmpty() ) {
      con = ...;  //make a new DBConnection
      } else {
      con = (java.sql.Connection) pool.elementAt(0);
      pool.removeElementAt(0);
   }
   return con;
   }

   public synchronized void putbackDBConnection( java.sql.Connection con ) {
      if (pool.size() < 8) {  // 8 is the MAX number of my pool, it also can be
100, 101...
      pool.addElement(con);
   }else{
   ... //close this con
   con=null;
   }
   return;
   }

   public synchronized void closeAllDBConnection() {
   String con=null;
      if ( !pool.isEmpty() ) {
   con=(java.sql.Connection)pool.elementAt(0);
   ...// cole the con
    con=null;
    pool.removeElementAt(0);
   }
   }

   protected synchronized void finalize(){
   closeAllDBConnection();
   super.finalize();
   }
}

[b]    ********how to use this simple DBConnection pool  in Servlet
class********

public class servlet_outsideDBCPHoder extends HttpServlet {
private static outsideDBCPHoder myDBCP
                      =outsideDBCPHoder.getOnlyOneInstance();

    //init ...

    _service/doGet/doPost{
   java.sql.Connection con= myDBCP.getDBConnection();

   ...// use con

   myDBCP.putbackDBConnection(con);
   }

   protected synchronized void finalize(){
   myDBCP.closeAllDBConnection();
   super.finalize();
   }

}

[c]    outsideDBCPHoder is a *SingleTonBean* ->
   *  the reason for making it  as a SingleTon is:
       try to avoid the problem led by loading/unloading MyServlet several
       times, but perhaps it is not necessary, I am not sure.
   *  the reason for that make it Serializable is:
      from the emails in Servlet-List, bean class will be loaded by
      the same ClassLoader, I also don't know if it is nessary.




Bo
Nov.23, 2000

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to