Hi Sanjay,
   you can avoid the problem by using the double-checked locking pattern
(Schmidt-Harris) as follows:

public static ConnectionPoolAsSingelton getInstance() {

                // check for null singleton
                if(instance==null)
            {
                        // obtain lock
                        synchronized( this )
                        {
                                // check for null again in case initialization
                                // already occurred in another thread
                                if(instance==null)
                                {
                                        return new ConnectionPoolAsSingelton();
                        }
                        }
                }
                else
                  return instance;
      }

the synchronization overhead is limited by the first null check,
so that further calls to the method never reach the synchronization code
and its associated overhead.

Regards,

Jin

-----Original Message-----
From: Radhakrishnan, Sanjay (c)
[mailto:[EMAIL PROTECTED]]
Sent: Thursday, April 13, 2000 10:08 AM
To: [EMAIL PROTECTED]
Subject: Re: Connection Pool


Hi Thomas,

        I have 2 questions,

        1. the protected instance in the Singleton class should be static
right? and Im sure thats what you meant.

        2. Also shouldn't you make both the get and put methods thread safe.
You've mentioned that only the Put method should be thread safe. Can i
Synchronize on the static instance to make the methods threadsafe

Thanks for your time
Sanjay

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, April 04, 2000 10:53 AM
To: [EMAIL PROTECTED]
Subject: SV: Connection Pool


Hi Sanjay
The best way is to implemets a Singelton (see the GoF book) that uses a
ConnectionPool.

public class ConnectionPoolAsSingelton {
      protected ConnectionPoolAsSingelton instance; // Store the
Singelton here

      private ConnectionPoolAsSingelton(...) { // yes private
constructor
      ....
      }
      public static ConnectionPoolAsSingelton getInstance() {
            if(instance==null)
                  return new ConnectionPoolAsSingelton();
            else
                  return instance;
      }
      //Put methods for your connpool here but keep in mind that this
code should bee thread safe.
}

to get an instance of connectionpool just call the static function
      ConnectionPoolAsSingelton pool =
ConnectionPoolAsSingelton.getInstance();

This should work fine but if you have any problems pleas email me att
[EMAIL PROTECTED]

/Thomas







   -----Ursprungligt meddelande-----
   Fran:       [EMAIL PROTECTED]
               [SMTP:[EMAIL PROTECTED]]
   Skickat:    den 4 april 2000 15:57
   Till:       [EMAIL PROTECTED]
   Amne:       Connection Pool

   I am trying to set up a connection pool for my JSP Project. Now I
   know that
   there are few ways to implement a connectionpool.  My first thought
   was to
   put in the the servlets init method,But then what do i do for my
   other
   servlets/JSPs, do i create a connectionpool for every servlet?.

   Is there a standard technique for implementing connection pools.  I
   think
   the best solution would be to have it in a common place which all the
   JSPs
   and servlets can access, something like a global.asa file in ASP.

   I would like to hear your thoughts on this.

   Thanks for your time
   Sanjay

   =====================================================================
   ======
   To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
   JSP-INTEREST".
   Some relevant FAQs on JSP/Servlets can be found at:

    http://java.sun.com/products/jsp/faq.html
    http://www.esperanto.org.nz/jsp/jspfaq.html
    http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
    http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
    <<Fil: Connection Pool.TXT>>

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to