• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Set me straight on synchronization for stateless bean

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've seen in the official J2EE tutorial that it lists synchronizing methods and objects as a way to keep your code thread-safe. However I've been told on the official Java forums that this is not a 100% safe solution because the J2EE container can load more than one instance of a servlet into the container at a time and since synchronization only works on a single object this technique is not 100% thread-safe.

Have I been misinformed?
 
Ranch Hand
Posts: 68
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only one instance of a servlet per application can be loaded into the Container. However request and response objects are created per request and thus it is thread-safe.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think that's quite true. The container can instantiate multiple servlets of the same type if it wants to.

Anyway, you want your web application to be able to run multiple threads at the same time, so synchronising all the methods will damage the performance. Instead, you should just be careful to ensure that multiple requests do not interfere with each other, and just synchronise things that needs to be synchronised.
 
Neeraj Dhiman
Ranch Hand
Posts: 68
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Rob. Can you explain ?

The container can instantiate multiple servlets of the same type if it wants to.

.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not Rob.

I was just pointing out that your statement "Only one instance of a servlet per application can be loaded into the Container" isn't true - there's nothing in the specification that says that. It might be true for some containers, but you shouldn't rely on it.
 
Sheriff
Posts: 67620
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also have multiple instance of the servlet mapped in the deployment descriptor, which will cause more than one instance to be created.

Bottom line: write your servlet in a thread-safe manner, starting with avoiding read-write instance variables.
 
Rob Micah
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While my servlet is fine, it's the code from a Stateless JavaBean it calls that needs to "synchronized" or configured to only run one at a time. What's the best way to handle this?
 
Neeraj Dhiman
Ranch Hand
Posts: 68
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh sorry Matthew for wrong name :-)

and Thanks for clearing my doubt.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic