Are Local Object references thread safe?

Hi All,

I have a static method which has lots of local objects which are basically references to nodes of an static DOM object which had been created at the application startup time..

I want to know if multiple threads access that static method,will the local objects inside the method be thread safe.

where are the local objects stored in a static method.are they specific to the thread's stack or they also are class level variables.My servlet calls the getData method multiple times i.e for each node and also multiple users call the servlet at the same time....

My code is as follows:

public static ArrayList getData(String objectName,String attributeName)

{

Node ndDoc = null;

ndDoc = getXMLDoc();

Node ndRoot = ndDoc.getFirstChild();

NodeList ndFirstList = ndRoot.getChildNodes();

System.out.println("This is the number of child nodes present:"+ndFirstList.getLength()+":::"+ndFirstList.item(0).getNodeValue());

ArrayList returnResult = null;

for(int i=0;i<ndFirstList.getLength();i++)

{

if(ndFirstList.item(i).getNodeName().equals("ObjectDBMapping"))

{

NodeList ndObjectList = ndFirstList.item(i).getChildNodes();

for(int j=0;j<ndObjectList.getLength();j++)

{

if(ndObjectList.item(j).getNodeName().equals("Object"))

{

if(ndObjectList.item(j).getAttributes().getNamedItem("NAME").getNodeValue().equalsIgnoreCase(objectName))

{

NodeList ndFieldList = ndObjectList.item(j).getChildNodes();

for(int k=0;k<ndFieldList.getLength();k++)

{

if(ndFieldList.item(k).getNodeName().equals("field"))

{

if(ndFieldList.item(k).getAttributes().getNamedItem("NAME").getNodeValue().equalsIgnoreCase(attributeName))

{

synchronized(ObjectPersistanceXMLParser.class){

returnResult = new ArrayList();

returnResult.add(ndFieldList.item(k).getAttributes().getNamedItem("SOURCE_TABLE").getNodeValue());

returnResult.add(ndFieldList.item(k).getAttributes().getNamedItem("SOURCE_COLUMN").getNodeValue());

returnResult.add(ndFieldList.item(k).getAttributes().getNamedItem("TYPE").getNodeValue());

returnResult.add(ndFieldList.item(k).getAttributes().getNamedItem("PERSIST").getNodeValue());

returnResult.add(ndFieldList.item(k).getAttributes().getNamedItem("DEFAULT_VALUE").getNodeValue());

returnResult.add(ndFieldList.item(k).getAttributes().getNamedItem("PRIMARY_KEY").getNodeValue());

returnResult.add(ndFieldList.item(k).getAttributes().getNamedItem("VERSIONABLE").getNodeValue());

returnResult.add(ndFieldList.item(k).getAttributes().getNamedItem("PRECISION").getNodeValue());

returnResult.add(ndFieldList.item(k).getAttributes().getNamedItem("FOREIGN_KEY").getNodeValue());

returnResult.add(ndFieldList.item(k).getAttributes().getNamedItem("PARENT_TABLE").getNodeValue());

returnResult.add(ndFieldList.item(k).getAttributes().getNamedItem("PARENT_COLUMN").getNodeValue());

returnResult.add(ndFieldList.item(k).getAttributes().getNamedItem("LEVEL").getNodeValue());

}

return returnResult;

}

}

}

}

}

}

}

}

if(returnResult == null){

System.out.println("This is the current state of dom: ");

printDom((Document)ndDoc);

if(!attributeName.trim().equalsIgnoreCase("class") &&

!attributeName.trim().equalsIgnoreCase("operationFlag")&&

!attributeName.trim().equalsIgnoreCase("primaryKeyColumnName")&&

!attributeName.trim().equalsIgnoreCase("tableName"))

System.out.println("No DB Mapping found for Object "+objectName+" and attribute "+attributeName);

}

return returnResult;

}

public synchronized static Document getXMLDoc()

{

if(configXML == null)

{

if(System.getProperty("OBJECT-CONFIG-XML") == null){

System.out.println("System Property OBJECT-CONFIG-XML not found");

}

String filePath = System.getProperty("OBJECT-CONFIG-XML");

System.out.println("Reading CONFIG Xml from file - "+filePath);

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

try

{

DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

configXML = docBuilder.parse(filePath);

}catch(Throwable t)

{

int errorCode = 111;

String errorMsg = "Problem in parsing OBJECT-CONFIG-XML file";

System.out.println(errorMsg);

}

}else{

System.out.println("Config XML Loaded from memory.....");

}

return configXML;

}>

[4682 byte] By [mrutyunjaya] at [2007-11-27 10:00:51]
# 1
Local variables are threadsafe--each thread gets its own copy of locals.The objects that the variables point to may not be threadsafe--for instance, if some other thread has references to the same objects.
jverda at 2007-7-13 0:32:25 > top of Java-index,Core,Core APIs...
# 2

Method local variables are thread safe. However this fact does not matter with the code in your posting.

Each method call to getData() operates on the same DOM ( value of system property "OBJECT-CONFIG-XML")

DOM implementations are not thread safe. See : http://xerces.apache.org/xerces2-j/faq-dom.html#faq-1

HTH

-Prashant

cprashantreddya at 2007-7-13 0:32:25 > top of Java-index,Core,Core APIs...
# 3
Ok, I was wrong. In your code every call to getData() makes a new DOM of the XML, so its OK as far as DOM access goes.
cprashantreddya at 2007-7-13 0:32:25 > top of Java-index,Core,Core APIs...
# 4

Hi,

please use [code] tag when posting more than one line of code.

As for "I want to know if multiple threads access that static method,will the local objects inside the method be thread safe."

Posted code seems to be OK untill You have modifiers for the same XML file.

Good Luck ;)

_Dima_a at 2007-7-13 0:32:25 > top of Java-index,Core,Core APIs...