• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Set me straight on synchronization for stateless bean

 
Rob Micah
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • 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?
 
Neeraj Dhiman
Ranch Hand
Posts: 68
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4567
8
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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: 98
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Oh sorry Matthew for wrong name :-)

and Thanks for clearing my doubt.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic