Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Spring singleton Beanscope

 
Greenhorn
Posts: 10
Hibernate Spring Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

As we know by default, spring bean is a singleton. Please help me understand what it means to be a singleton when multiple users are access an application concurrently ? Will each user have a separate instance of a bean or will all the users share the same bean ?

Can we call it a singleton if each user can have a separate instance created for a bean? Please clarify this.

Regards,
Yed.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the spring context singleton doesn't mean that you can create only one instance, but it means that there is only one instance (nothing stops you to create more).
It works like this, on start up, context is read, all the beans are created and are available in the application. So you have only one instance of each bean when your application is running, so they are accessed by multiple threads.
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is only one instance created for the entire application. Not per user. Many threads can run the same single instance at the same time, unless you make a bean from a class that is holding state and each user has their own set of state they are expecting. Then just like in non-Spring apps, you have to deal with threading issues. If your beans don't hold state, like your Services, Repository/DAOs, then a single instance is plenty to support as many User threads as you like.

Mark
 
Yed Su
Greenhorn
Posts: 10
Hibernate Spring Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark and Pete. So as per my understanding, if we talk about singleton scope in a web application bean, each user who access the web app will have their own thread which runs the same single instance of a singleton at same time. Spring container will handle the synchronization issues i. e. as long as the thread is accessing the instance it is locked and the remaining threads will keep waiting for the lock to be released. If this is the case, surely it should be a performance issue.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think its not thread safe, for spring single tones.
 
Mark Spritzler
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yed Su wrote:Thanks Mark and Pete. So as per my understanding, if we talk about singleton scope in a web application bean, each user who access the web app will have their own thread which runs the same single instance of a singleton at same time. Spring container will handle the synchronization issues i. e. as long as the thread is accessing the instance it is locked and the remaining threads will keep waiting for the lock to be released. If this is the case, surely it should be a performance issue.



No.

There is no synchronization being done. There doesn't need to be any, because these beans shouldn't be holding state. So a Controller, Service, Repository/DAO that many user threads are running don't hold state and are therefore threadsafe and don't need to have synchronization blocks.

Mark
 
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic