This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does Spring singleton beans handle too much traffic?

 
Fawad Ali
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks in advance.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Fawad Ali
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Once again, thanks for your response.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, Thats amazing. I didnt know singleton does that. How dumb I am.

Thanks a lot.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic