wood burning stoves 2.0*
The moose likes Servlets and the fly likes Servlets, Variables, and Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets, Variables, and Threads" Watch "Servlets, Variables, and Threads" New topic
Author

Servlets, Variables, and Threads

al langley
Ranch Hand

Joined: Mar 28, 2008
Posts: 35
I have a question regarding threads and servlets.


Say I have a servlet called TheServlet that uses non-servlet class named TheDatabase, whose job it is to query a database and return a String based on some query.
Something like this:



When it comes to multi thread and synchronization issues, I think I probably have to lock access to the session object, as well as the instance object theDatabase. But to prevent multithreading issues with theDatabase instance, is using the synchronized keyword the only way to handle this, or is there a better way to design this. For example, making TheDataBase a local variable in the doPost method (instead of an instance variable), or even taking the code from TheDatabase class and sticking it in the doPost method itself.
Thanks for taking the time to read this question.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

Why are you creating an instance of TheDatabase as an instance variable? That's just asking for trouble.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
al langley
Ranch Hand

Joined: Mar 28, 2008
Posts: 35
Thanks for the response Bear..and thats a good question! The short answer is that I like to live dangerously---I also run with scissors...actually, its inexperience. I was trying to think of an example (in this case the fictional TheDatabase class) whose setup/initialization might take longer or be complicated, and therefore you might only want to call the constructor TheDatabase() once when the servlet is originally created?
But I'm guessing by your response that an instance variable brings more problems than its worth.
So, I guess the answer is, use local variables?
[ April 07, 2008: Message edited by: al langley ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

Originally posted by al langley:

But I'm guessing by your response that an instance variable brings more problems than its worth.
Absolutely. Most experienced web developers would never entertain read/write instance variables to solve the problem you state. Rather, a single instance of the "expensive" class would be created and stored in application context. Of course, because it is shared, synchronization is necessary when read/write access could cause threading issues. But instance variables? No.

So, I guess the answer is, use local variables?
When possible, yes. Ergo, no threading issues.
al langley
Ranch Hand

Joined: Mar 28, 2008
Posts: 35
Thanks Bear! Much appreciated.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlets, Variables, and Threads