Results 1 to 8 of 8

Thread: Is bean scope is thread safe ?

  1. #1

    Default Is bean scope is thread safe ?

    hi, I got some question.

    As I saw in the sping documentation section 3.4 " use singleton for statelss and use prototype for stateful "(my understand could be wrong), I though that prototype should be stateless ? since stateless always thread safe.

    what is the common pratice (or correct way) to do set bean scope in a web app?
    If i have a service which is connect to datasource, set the service to singleton is that mean it will only live in the current thread?
    Say I have 2 http request, will this 2 request use the same service if the bean scope is singleton?
    If this the case, should I change to "prototype" (like create a new object for every new thread) ? or "request" scope is the one I should using? what is the diference ?

    Whenever we call getBean("bean") in a multi-thread , if it is a singleton scope, will it create new object for each thread, or it just behave like a GoF singleton, which is only use the same singleton bean ?

    thanks for help !

  2. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    "singleton" means the same bean will be returned for each getBean() invocation. "prototype" means, a new bean will be created for each getBean() invocation.

    That said, I'd like to add that wiring occurs usually upfront, meaning you will not have getBean() invocations in your application code.

    As of the state: If you use a "singleton" bean, it should be stateless as it might be shared by multiple other beans. If the are stateless they are usually also thread-safe.
    Prototype beans *might* be stateless as well, but they normally hold state. Be careful what you do with them inside of your application. If you pass them around and expose them to other threads, you have to make them thread-safe yourself.

    Have a look at Spring's reference manual for more details on scopes.

    Regards,
    Andreas

  3. #3

    Default

    hi, thx for reply.

    I read the documentation, but stil not quite understand how to apply it.

    I just wondeting that in typical web application, should we use prototype or singleton scope for DAO service?

    If we use prototype, it will create a new object for every http request ? am I right ?

  4. #4
    Join Date
    Sep 2004
    Posts
    1,086

    Default

    Singleton scope creates one bean instance no matter what. Prototype scope creates one bean instance per injection and per getBean() invocation.

    Both scopes have nothing to do with threading (directly), they only allow YOU to use them in different ways so that it fits your needs.

    DAOs are typically stateless and there is no need to make them prototypes.

  5. #5
    Join Date
    Apr 2008
    Posts
    1

    Default

    But if a DAO is a singleton that may cause a performance problem, couldn't it? Let's say we have multiple clients access a DAO method simultaneously, that takes a longer time to process. Say a "SELECT" statement takes 10 seconds. Three clients access the dao.getList() method at the same time. Wouldn't it take 30 seconds for the third client?
    How can this be avoided?

  6. #6
    Join Date
    Sep 2004
    Posts
    1,086

    Default

    No. Singletons != synchronisation. You have to (manually) synchronize if they are not threadsafe, but per default you will have multiple threads running through the same method concurrently.

  7. #7
    Join Date
    Feb 2008
    Location
    India
    Posts
    6

    Default For Web Apps you should be using request/session

    If I understand your question correctly, you are actually looking for a thread-safe scoped beans for your web application.

    For that you need to go for scope as either request/session, which ever suits your requirement. Prototype doesn't work for you in this context...

    Let me know if you need a code snippet to do so ...

    Regards,
    Seshendra Nalla.

  8. #8

    Default

    yes... i think the scope should be request or session.

    But if I use annotation @Controller, DO anyone know how to set the bean scope ?

    <context:component-scan base-package="org.springframework.samples.petclinic.web "/>

    any idea ??

    kiwi
    ----
    happy hacking !
    Last edited by kiwi; Apr 16th, 2008 at 04:49 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
  •