Results 1 to 4 of 4

Thread: Prototype beans for web applications and web services

  1. #1

    Default Prototype beans for web applications and web services

    I have experienced a some issues with web applications and web services when using prototype beans.

    One simple example is where a controller declared as a singleton bean uses an entity prototype bean. No matter what, the entity prototype bean is treated like a singleton. This is the case because when the entity prototype bean is destroyed (nulled) the application breaks.

    If I set the controller to prototype for the above, then it works fine as new beans are instantiated.


    The next complex example is using a command pattern and concurrency. This is going to be a lengthy one, so here goes...

    Say, having a couple command objects like so:


    public class GetUserCommand implements Runnable {

    private User userValue; // For example purposes to an entityBean.
    private ComponentBean componentBean; // For example purposes.

    private ComponentBean getComponentBean() {
    return componentBean;
    }

    private void setComponentBean(ComponentBean componentBean) {
    this.componentBean = componentBean;
    }

    public void execute() {
    userValue = componentBean.getUser();
    }

    public User value() {
    return userValue;
    }
    }


    public class GetDataCommand implements Runnable {

    private Data dataValue; // For example purposes to an entityBean.
    private ComponentBean componentBean; // For example purposes.

    private ComponentBean getComponentBean() {
    return componentBean;
    }

    private void setComponentBean(ComponentBean componentBean) {
    this.componentBean = componentBean;
    }

    public void execute() {
    dataValue = componentBean.getData();
    }

    public Data value() {
    return dataValue;
    }
    }



    They are declared as singleton beans for use by a controller (which is also singleton). The controller would implement concurrency, utilising thread pool. After executed each command simultaneously, awaiting for each thread to complete and then return values from each command object, the values are retained. To resolve this, the values are nulled when the task is complete.

    However, the problem I found with the above developing a web service (using xfire) is that when multiple users hit the web service simultaneously it is possible for the command bean to hold and return the value to the wrong user! To overcome this I have set each command beans, the controller bean AND XFireExporter bean as prototypes, then using mapping for SimpleUrlHandlerMapping. This works fine.

    So... my concerns are:

    1) I feel the above solutions I've implemented is not the best practice for using prototype beans. So what is the best practice?

    2) I also feel that the above can lead to memory leakage issues, as I understand the lifecycle for prototype beans changes. Is this the case?

    3) How come prototype beans behave like singletons behind any singleton controller bean?

    4) Any other thoughts on this one? Am I missing the point?


    Please correct me for any mistakes I've made. Many thanks in advance for your views on this.


    Shah.

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Is it possible take a step back. What is the problem you are trying to solve? What do you want to do. If you can break it down people might be able to help.

  3. #3

    Default

    Well, a couple of things I want to resolve.

    1) I want to ensure that users do not see other user's data when developing web applications and/or web services. Using prototype entity beans controlled by singleton controller beans I've noticed that this does occur.

    2) I'm hoping to get a better understanding of prototype beans and how to use it correctly. Things like best practice, how to handle the lifecycle of prototype beans, the meaning of singleton beans being stateless, etc.


    Shah.

  4. #4

    Default

    I think I've figured out the appropriate practice.

    From what I now understand, its probably not ideal to set up entity beans or command object beans at all! Its much simpler to instantiate these objects whenever its required within a given method call, and then dispose of it after use.

    Much easier to handle in this way, thus ensuring that users don't see the wrong data.

    Still, I would like people's views between singleton and prototypes, and the appropriate usage of prototype beans.


    Shah.

Posting Permissions

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