View Full Version : Is a intermediate layer object thread safe
A Kumar
Aug 10th, 2007, 02:46 PM
Hi,
If there is a service object between a controller and a DAO,will it be thread safe...
What should we do to make it thread safe in case....it isn't?
Regards
karldmoore
Aug 11th, 2007, 03:43 AM
If the service is stateless then that should be a good start. What's the specific problem you are having? Can you post the code you are having problems with?
A Kumar
Aug 11th, 2007, 04:09 PM
Here is the trimmed code...
public MyController implements Controller{
public ModelAndView handleRequest(....){
AcService svobject=new AcServiceobject();
Arraylist list= svobject.getResponse(hashmap); //call service layer object
//hashmap contating details from request
//Steps to populate the response into the model
return new ModelAndView("Account",accountdetails,"Accoundetails");
}
}
public class AcServiceobject{
public Arraylist getResponse(Hashmap hashmap){
//call some methods in this class...to check for the request values
//and create the order of the objects(domain object)
//For this particular request
Call DAO and get the response
//Call methods in this class..to format the response and add to list
return arraylist;
}
}
public class..MyDAO...{
}
karldmoore
Aug 12th, 2007, 05:14 AM
If you are creating a new AcServiceobject object everytime then yes it will be thread-safe. I'm not sure however that you want to be creating that. If you are using Spring then you can tell it to inject the reference in for you instead. Have a read of the reference manual for more information.
A Kumar
Aug 12th, 2007, 01:47 PM
Thanks karldmoore!!!...
Are you suggesting that I rewrite the code in the below manner such that spring injects the AcServiceObject instance @ runtime for each request to the controller thereby making it thread-safe.
<bean id="myDao" class="MyDAO" singleton="true">
</bean>
<bean id="acServiceObject" class="AcServiceobject" singleton="false">
<property name="myDao"><ref bean="myDao"/></property>
</bean>
<bean id="myController" class="MyController" singleton="true">
<lookup-method .. ref="acServiceObject"/>
</bean>
Regards
karldmoore
Aug 12th, 2007, 04:14 PM
Are you suggesting that I rewrite the code in the below manner such that spring injects the AcServiceObject instance @ runtime for each request to the controller thereby making it thread-safe.
I am suggesting that you inject the reference instead. However unless you have some state in the service, why would you need a new one for every request?
A Kumar
Aug 12th, 2007, 10:08 PM
I am suggesting that you inject the reference instead. However unless you have some state in the service, why would you need a new one for every request?
I am suggesting that you inject the reference instead(didnt get this one..)
Yes,I do have some state in the service and so i create a new one for every request.
karldmoore
Aug 13th, 2007, 08:10 AM
Ok so if you have state, you need a new one everytime. Scoped proxies might help you out here. Likewise if you want to inject references to other beans, the manual has examples of this.
http://www.springframework.org/docs/reference/beans.html#beans-factory-scopes-other
http://www.springframework.org/docs/reference/beans.html#beans-ref-element
A Kumar
Aug 13th, 2007, 10:13 AM
Ok so if you have state, you need a new one everytime. Scoped proxies might help you out here. Likewise if you want to inject references to other beans, the manual has examples of this.
http://www.springframework.org/docs/reference/beans.html#beans-factory-scopes-other
http://www.springframework.org/docs/reference/beans.html#beans-ref-element
I have to use spring 1.2.5 for the dev..and i dont think that it supports scopes..
Regarding the new objects for every request...
is this one better or should we go for setter based prototype injection using references..as mentioned by you..
<bean id="myDao" class="MyDAO" singleton="true">
</bean>
<bean id="acServiceObject" class="AcServiceobject" singleton="false">
<property name="myDao"><ref bean="myDao"/></property>
</bean>
<bean id="myController" class="MyController" singleton="true">
<!-- calls createMyServiceObject method in the handleRequest method for a new instance for each request-->
<lookup-method .. ref="acServiceObject"/>
</bean>
Regards
Jörg Heinicke
Aug 13th, 2007, 10:15 PM
Isn't that exactly the same question (http://forum.springframework.org/showthread.php?t=41467) we had recently?
Joerg
A Kumar
Aug 14th, 2007, 03:28 AM
yes..
sort of ...same....question....just that the issue of thread safe objects was nagging me...and thought of clarifying it...
neverthless....thanks for the posting that....link to earlier.
.I was able to group both the posts....for my answer....
Regards :)
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.