Announcement Announcement Module
Collapse
No announcement yet.
Thread Safety question related to jaxb2Mashaller and SpringMVC Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Thread Safety question related to jaxb2Mashaller and SpringMVC

    Hi,

    I am new to developing web apps with java and Spring Framework. I have read about thread safety being an important point that needs to be considered while using java for web application development and have fairly understood the reasons behind it. However, while doing the actual coding, I am unable to figure out which beans will be Singleton and which will not.

    In my case, Jaxb2Marshaller is used for working with xml data in a REST application. A Customer class annotated as @XmlRootElement is as follows -

    Code:
    @XmlRootElement(name="customer")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Customer implements RequestNode {
    	
    	private List<String> validationErrors;
    	
    	@XmlElement(name="name")
    	private String name;
    	
    	@XmlElement(name="email")
    	private String email;
    	
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getEmail() {
    		return email;
    	}
    	public void setEmail(String email) {
    		this.email = email;
    	}
    	
    	public Boolean validate() {
    		
    		this.validationErrors = new ArrayList<String>();
    		
    		if(this.name == null || this.name.equals("")){
    			this.validationErrors.add("101");
    		}
    		
    		if(this.email == null || this.email.equals("")){
    			this.validationErrors.add("102");
    		}
    		
    		if(this.validationErrors.isEmpty()){
    			return true;
    		}
    		
    		return false;
    	}
    	
    	@Override
    	public List<String> getValidationErrors() {
    		return this.validationErrors;
    	}
    	
    }
    And the controller action is as follows

    Code:
            @RequestMapping(method=RequestMethod.POST, value="/generate")
    	public String generate(@RequestBody String body, Model model){
    		
    		Source source = new StreamSource(new StringReader(body));
    		
    		Customer c = (Customer) this.jaxb2Mashaller.unmarshal(source);
    		
    		//TODO generate response and add response obj to model			
    		
    		return "mytemplate";
    	}
    Since the Customer class instance is being created by jaxb2Mashaller , its not clear whether an instance variable in the Customer class will lead to thread safety issues.

    So in general my question is, while using Spring, is it possible to tell in which classes instance variables can be used and where they need to be avoided ?

    Thanks
    Last edited by naiquevin; Feb 18th, 2011, 03:44 AM.

  • #2
    Use [ code][/code ] tags when posting code that way it remains readable.

    Your current setup doesn't lead to any problems a customer is simply recreate when you call unmarshal (it gives you a new Customer)...

    Comment


    • #3
      Thanks for the reply.

      Comment

      Working...
      X