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 05:34:18 GMT
1.  Is it my imagination or was the same message posted 5 times?
2. If I understand correctly, the problem is that multiple users are 
getting the same DOM object?
    a. Question: Why do you have two actions that do essentially the 
same thing? 
    b. By the way, even though you declared them ThreadSafe they clearly 
are not.  It is possible for the web-browser to issue two requests at 
the same time to Cocoon for the same user, depending on how the html 
page is constructed, in which case both of these actions could execute 
at the same time.
    c. What is the purpose of synchronizing the object returned by 
newInstance?  That is a local variable and if each call to newInstance 
returns a different object, this will accomplish nothing. If you want to 
serialize the call to newDocument then you should refactor this whole 
block of code in a method in a utility class and then always call that 
method from all your actions to get the document. It should do something 
like:

private static final String LOCK = "Lock";

Document getDocument()
{
         Request request = ObjectModelHelper.getRequest(objectModel);
         Session session = request.getSession(true);
         Document doc = (Document)session.getAttribute("myAttribute");
         if   (doc == null)
         {
              // It is possible for more than one thread to get here at 
the same time with doc == null
              syncronize(LOCK);
              {     
                    // So check again. Only the first caller should 
actually create the document.
                    doc = (Document)session.getAttribute("myAttribute");
                    if (doc == null)
                    {     
                        DocumentBuilderFactory dbf  = 
DocumentBuilderFactory.newInstance();                                 
                        doc = dbf.newDocumentBuilder().newDocument();
                        session.setAttribute("myAttribute", doc);
                     }           
               }
         }
         return doc;
}

Mime
View raw message