File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Set me straight on synchronization for stateless bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Set me straight on synchronization for stateless bean" Watch "Set me straight on synchronization for stateless bean" New topic
Author

Set me straight on synchronization for stateless bean

Rob Micah
Ranch Hand

Joined: Aug 30, 2011
Posts: 94
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

Joined: Dec 19, 2011
Posts: 63

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.


Correct Me if i am wrong
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4341
    
    7

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

Joined: Dec 19, 2011
Posts: 63

hi Rob. Can you explain ?
The container can instantiate multiple servlets of the same type if it wants to.
.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4341
    
    7

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

Joined: Jan 10, 2002
Posts: 60763
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rob Micah
Ranch Hand

Joined: Aug 30, 2011
Posts: 94
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

Joined: Dec 19, 2011
Posts: 63

Oh sorry Matthew for wrong name :-)

and Thanks for clearing my doubt.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Set me straight on synchronization for stateless bean
 
Similar Threads
Webserver request problems
Thread-safe global variables
Passed SCEA - 1 with 87%
About vectors.
Difference between Thread-Safe and Synchronization