Results 1 to 3 of 3

Thread: Struts, Spring and Thread Safety Issue

  1. #1
    Join Date
    Sep 2004
    Location
    Bangalore / India
    Posts
    7

    Default Struts, Spring and Thread Safety Issue

    Hi,

    Iam using struts + spring + hibernate


    I have an Action class , to which I pass the command object like this

    This entry goes into my action-servlet.xml file
    <bean name="/ReadOnlyAction" class="org.med.core.Command.ReadOnlyAction" singleton="false">
    <property name="commandObject"><ref bean="productDAO"/></property>
    </bean>


    This entry goes into my struts-config.xml file
    <action path="/ReadOnlyAction" type="org.springframework.web.struts.DelegatingAct ionProxy">
    <forward name="menu" path="/core/order.jsp"></forward>
    </action>


    Now If you notice the action class, the DAO is set as instance variable of my
    Action class , but struts strongly discourages this kind of design,

    In my application the same action class might get called with diffrent DAO impl,
    does this pose any danger to the thread safety etc, as Struts creates only one
    instance of action classes.

    Is this the best way to inject Spring managed beans into Struts Action classes ?

    public class ReadOnlyAction extends Action {

    /**
    * @return Returns the commandObject.
    */
    public Command getCommandObject() {
    return commandObject;
    }
    /**
    * @param commandObject The commandObject to set.
    */
    public void setCommandObject(Command commandObject) {
    this.commandObject = commandObject;
    }
    Command commandObject = null;
    ...
    ...
    ..
    .
    Hari

  2. #2
    Join Date
    Aug 2004
    Location
    Denver
    Posts
    249

    Default Re: Struts, Spring and Thread Safety Issue

    Quote Originally Posted by hariharan1005
    Now If you notice the action class, the DAO is set as instance variable of my
    Action class , but struts strongly discourages this kind of design,
    Spring is handling the creation of your actions and since you have singleton="false" it will create a new Action for each request. Therefore, you don't need to worry about instance variables. If you were developing a "tranditional" Struts application - where Actions are single-threaded - you would have to worry about this.

  3. #3
    Join Date
    Oct 2004
    Location
    Atlanta, GA
    Posts
    38

    Default Properties versus model

    One thing to clarify here is the difference between setting controller bean properties (via dependency injection) versus passing objects in the model to a view. It is my understanding that the bean properties on a controller should be treated much like final variables; they provide useful information to the controller or provide a handle to a service, but should not be mutable. For data that changes on a per-request basis, objects should be passed in the model part of the ModelAndView (or, in the case of Struts, passed directly as request attributes). Using this strategy, you can make the bean a singleton and save the overhead of creating the bean on each request.

    Hope that helps,
    Derek

Similar Threads

  1. Thread Safety and Struts-Spring Integration
    By hariharan1005 in forum Web
    Replies: 2
    Last Post: May 16th, 2006, 04:04 AM
  2. A Spring Class Loader?
    By azzoti in forum Architecture
    Replies: 8
    Last Post: May 7th, 2005, 04:02 AM
  3. Replies: 3
    Last Post: Apr 14th, 2005, 11:59 PM
  4. Sessions closing after commit
    By bendg25 in forum Data
    Replies: 0
    Last Post: Mar 21st, 2005, 04:38 AM
  5. JdbcTemplate singleton & thread safety
    By zerzio in forum Data
    Replies: 5
    Last Post: Feb 7th, 2005, 12:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •