cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ralph Goers <Ralph.Go...@dslextreme.com>
Subject Re: Problem with sharing sessions/ multithreading
Date Sun, 17 Apr 2005 17:06:45 GMT
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

Mime
View raw message