Mar 13th, 2008, 04:05 AM
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 !
Mar 13th, 2008, 05:34 AM
"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.
Mar 14th, 2008, 02:12 AM
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 ?
Mar 14th, 2008, 06:12 AM
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.
Apr 15th, 2008, 06:22 AM
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?
Apr 15th, 2008, 06:27 AM
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.
Apr 15th, 2008, 07:22 PM
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 ...
Apr 16th, 2008, 04:46 AM
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 ??
happy hacking !
Last edited by kiwi; Apr 16th, 2008 at 04:49 AM.