Hi Marc,

I would say that your design about using Singleton Pattern is
appropriate.Also your method is Thread safe as far as  EventLogController
is concerned... But if the shared resouce is accessed from any other place
in your web application then your design will not be full-proof. I mean if
the shared resource is a private member for EventLogController then your
design is perfect...

Regards,

Daljit Singh

-----Original Message-----
From:   Marc Krisjanous [SMTP:[EMAIL PROTECTED]]
Sent:   Friday, November 24, 2000 5:18 AM
To:     [EMAIL PROTECTED]
Subject:        Singleton Instance in Multithreading (servlet) - can it work?

Hi all,
I believe my last email about the Singleton V.s static question requires a
more clear explanation:

I have a class that writes data to a shared resource within my web
application. All components within the web application use this class to
write data to the log file.  The class uses the Singleton Instance design
pattern.

Question
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Is this good design? here are a set of steps that would happen (I have
provided skeleton class defs below):

1. Request to servlet sent from web page.

2. The servlet calls the init method if first request.  The init method
loads the Servlets instance var with a call to the EventLogController's
getInstance method.

3. The servlet then writes to the log file by calling the
EventLogControllers sync writeToEventLog().

4. The servlet then creates an instance of another component.

5. The component also has an instance var that upon creation gets a handle
on the EventLogController (same as the servlet).

6. The component then attempts to write to the log file using the same
method call.

Now... Is this approach thread safe?? Can it handle more than one post or
get request at the same time??

Best Regards

Marc


public class EventLogController {

        private static HPDFv2.EventLogController theInstance = null ;

        public synchronized boolean writeToEventLog(String fileName, String
message)
        {
        //write to log file
        }

        public static EventLogController getInstance()
        {
                if( HPDFv2.EventLogController.theInstance == null){
                        HPDFv2.EventLogController.theInstance = new
HPDFv2.EventLogController();
                }

                return HPDFv2.EventLogController.theInstance;

        }

}//end class def


public class IOControlServlet extends HPDFv2GenericServlet{

        private HPDFv2.EventLogController myController = null;

        public void init() {

                if(myController == null){

                myController = HPDFv2.EventLogController.getInstance();

                }
        }//end init


        doGet(.......){

        myController.writeToEventLog(getDebugLogName(),"IOControlServlet:
DEBUG is On!");

        MyComponent c1 = new MyComponent();
        //c1 also has a global ref to the EventLogController and the
component also writes to the log file.

        }


}//end servlet class

___________________________________________________________________________
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

___________________________________________________________________________
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