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;
}>

