Torsten Curdt wrote:


Don't want to be picky ...but better don't use DCL

http://www.javaworld.com/javaworld/jw-05-2001/jw-0525-double.html

cheers


Thanks, that article wasn't too helpful in figuring out why the technique doesn't work. However, it refreences an article from Alan Holub that does. From his description, the following version of the DCL should work fine. I'm not sure the function actually has to be static. From what I could tell synchronized should be sufficient.

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 = buildDocument(dbf);
session.setAttribute("myAttribute", doc);
} }
}
return doc;
}


private static synchronized Document buildDocument(DocumentBuilderFactory dbf)
{
return dbf.newDocumentBuilder().newDocument();
}


--
Torsten





Reply via email to