I will try this.. because.. after using this utility class, I see sharing
sessions more often. when I switched the users in the same browser.




thanks

Kiran Kumar (Raj)
 
(502) 696-7203




-----Original Message-----
From: Ralph Goers [mailto:[EMAIL PROTECTED]
Sent: Sunday, April 17, 2005 1:07 PM
To: [email protected]
Subject: Re: Problem with sharing sessions/ multithreading


Kumar, Kiran wrote:

>here's what I did. and changing all the code to use this.
>==========================================
>       private static final String LOCK = "Lock";
>       
>       public Document getDocument(Map objectModel,String attrname)
>       throws ParserConfigurationException
>       {
>         Request request = ObjectModelHelper.getRequest(objectModel);
>         Session session = request.getSession(true);
>         Document doc = (Document)session.getAttribute(attrname);
>         if   (doc == null)
>         {
>              // It is possible for more than one thread to get here at 
>                         // the same time with doc == null
>              synchronized(LOCK)
>              {     
>                    // So check again. Only the first caller should 
>                                       //      actually create the
>document.
>                    doc = (Document)session.getAttribute(attrname);
>                    if (doc == null)
>                    {     
>                        DocumentBuilderFactory dbf  = 
>       
>DocumentBuilderFactory.newInstance();                                 
>                        doc = dbf.newDocumentBuilder().newDocument();
>                        session.setAttribute(attrname, doc);
>                     }           
>               }
>         }
>         return doc;
>       }
>=====================================
>
>but why do we need static method??  please guide me
>
>
>  
>
As Alan Holub pointed out, the JVM does not guarantee that the 
constructor for the Document returned by the call to newDocument() may 
not have been run at the time the Document object refrerence is assigned 
to the session attribute.  Simply putting the call to newDocument() 
inside its own synchronized block should address that problem.

Ralph

Reply via email to