Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Spring framework context and thread safety

 
Greenhorn
Posts: 2
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TL;DR: If singleton beans in Spring's context are not threadsafe, how do we manage multiple requests/threads accessing the service bean and not creating inconsistency?

Hi,
I have done some java SE coding and currently reading Spring Start Here by Laurentiu Spilca.
As we are introduced to prototype beans in section 5.2, thread safety is brought up and how mutable objects must be handled by prototype beans(instead of singleton beans) to avoid race condition. But my confusion is that the singleton bean(CommentService) that is creating the prototype beans(CommentProcessor) is not thread safe either. So to make it thread safe we will have to synchronize sendComment()? Would not this effect performance?



Thanks and if I am not clear please let me know
_M
 
Saloon Keeper
Posts: 15656
367
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Michael Straub wrote:TL;DR: If singleton beans in Spring's context are not threadsafe, how do we manage multiple requests/threads accessing the service bean and not creating inconsistency?


Singleton beans MUST be thread-safe, in every case.

But my confusion is that the singleton bean(CommentService) that is creating the prototype beans(CommentProcessor) is not thread safe either.


Why? What makes you think that CommentService is not thread-safe?

So to make it thread safe we will have to synchronize sendComment()? Would not this effect performance?


You must NEVER synchronize on singleton beans. To make them thread-safe, they must be stateless. You do this by making them immutable.

The CommentService class is immutable, and therefore thread-safe.
 
Saloon Keeper
Posts: 27926
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would be a little cautious stating that,

By default the Spring BeanFactory will construct and decant single instances of beans, so in the literal sense yes they are singletons. But they are NOT expected or required to be strict GoF-style Singleton objects.

Spring beans are often stateful, in fact. Where that may result in threading concerns, it is up to the bean itself to manage any sort of synchronization mechanisms required. Spring doesn't get involved. Spring's participation in threading and other asynchronous concerns is limited to optional Spring add-on modules, not the Spring core.

It's perfectly permissible for a bean managed by a Spring BeanFactory to define and enforce itself as a hard-code GoF singleton object, But it is definitely not required to, and usually not worth the extra work.
 
Michael Straub
Greenhorn
Posts: 2
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your explanations!

I realized that if singleton bean CommentService(which is stateless) asks the container for a prototype bean CommentProcessor, each thread will have its own copy of CommentProcessor, thus making it thread safe. No shared data among threads anymore! Neat trick
 
Stephan van Hulst
Saloon Keeper
Posts: 15656
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've got it!
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic