Announcement Announcement Module
Collapse
No announcement yet.
how does spring manage bean instance pooling Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • how does spring manage bean instance pooling

    Hi

    I would like to know how spring handles bean instance pooling.

    I know that by default, Spring creates all bean instances as singletons, that is only one bean instance is created and the same instance is returned for each request of that bean.

    But when i am developing a web application , if multiple users request simultaneously for the same bean , then how does Spring handle it.

    And also I would like to know how to get the benefits of Stateless Session Bean & Stateful Session bean without using ejb provided by spring(ie using only pojo's)

    Thanks in advance

  • #2
    But when i am developing a web application , if multiple users request simultaneously for the same bean , then how does Spring handle it.
    If you are using the default mode (singletons) then Spring will serve the same object to the requests. If not then Spring will keep creating objects. There are no concurrency problems here: you either have a single instance (singleton) or per-request instance - in either way from the initialization point of view there are no problems.
    There might be problem when executing the code with concurrent threads but that's your problem - if you use singleton then make sure you don't use class levels fields or you will run into trouble.

    And also I would like to know how to get the benefits of Stateless Session Bean & Stateful Session bean without using ejb provided by spring(ie using only pojo's)
    Stateless Beans (SLSBs) can be very simple and efficient replaced by singletons objects - you have only methods so running just a single instance of the class is no problem.
    For Statefull Beans (SFSB) I cannot give you an advice because I haven't used them and I try to work stateless beans as much as possible.

    I can't remember when there was a more complete explanation when pooling makes sense (try searching the forums) but the idea is that for a web application the web container is serving the requests and does the pooling.
    Thus there isn't much gain for doing pooling at Spring level - if the limit is reached the web container will stop serving requests in more elegant way so you don't have to worry about that at Spring level.

    Comment


    • #3
      how does spring manage bean instance pooling

      hi costin,

      thanks for this

      But i am still not clear
      I dont understand what concurrent threads you are talking about.
      Is it the multiple users trying to access the same bean simultaneously.

      For web applications should I use singletons or instance per request.

      If I am using singletons and if multiple users try to access the same bean simultaneously. Then how will it service the users.

      Comment


      • #4
        I dont understand what concurrent threads you are talking about.
        Is it the multiple users trying to access the same bean simultaneously.
        Multiple users mean multiple or concurrent threads. A bean it's a class - the same rules that apply with concurrency on a normal class apply also on the bean.
        Spring doesn't do any synchronization by default - it just server you a bean - a new one (non-singleton) or the same(singleton).


        For web applications should I use singletons or instance per request.
        It depends a lot of what are your requirements and frameworks. I use for example instance per request because I use webwork and that's what the framework expects. You have to design the system approapriately based on this decision.
        If I am using singletons and if multiple users try to access the same bean simultaneously. Then how will it service the users.
        I guess my explanation wasn't enough. Accessing the same instance of the bean it's the same as accessing the same instance of a class. If you want an explanation about this then I guess the Java tutorial or some books on concurrency can explain better then I can.
        Try to do a small example with only one instance and multiple thread accessing it and trying to do read/write.

        Comment

        Working...
        X