This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Hi All,
I know that Spring beans in application context are singletons by default. So how does the framework handles the singleton structure when there is too much traffic. For example, if I have bean named daoManager for accessing database and its singleton. And if there are too many users accessing the database layer, then the single instance of daoManager will probably get slowed. So how does spring handles this situation? Does it create a new instance of the daoManager or it will just die out?
Nope. Traffic has no impact on the object. If the DAO doesn't hold any state, which it shouldn't. Then it is all based on how many threads the OS can handle. And Another wait point could be your Connection Pool setting. But regardless of both those has absolutely no affect on the Spring bean. It can handle as many threads all at once.
If your bean is holding state, then you have thread issues because more than one thread could be changing that state, then you have to do synchronization. That is why you should have the DAO hold state.
Mark:
Thanks for your replies. My concern is that if I have a function named getABC() in a singleton bean. If it is invoked for user A and it takes a processing time close to infinity(too many loops etc). Then the second user B will have to wait until the user A request is not finished. Is it like that?
This may not be a question related to Spring but to singleton pattern but as I have started the thread here so let me ask you this.
Fawad Ali wrote:Mark:
Thanks for your replies. My concern is that if I have a function named getABC() in a singleton bean. If it is invoked for user A and it takes a processing time close to infinity(too many loops etc). Then the second user B will have to wait until the user A request is not finished. Is it like that?
Not at all. Each user can call the same method at the exact same time and all run simultaneously. Each user gets their own thread of execution.
Mark
Fawad Ali
Ranch Hand
Joined: Dec 28, 2009
Posts: 109
posted
0
Wow, Thats amazing. I didnt know singleton does that. How dumb I am.
Fawad Ali wrote:Wow, Thats amazing. I didnt know singleton does that. How dumb I am.
Thanks a lot.
Has nothing to do with Singleton. Has to do with a Java object. I can have an instance and if that object doesn't hold state, I don't have any threading issues.