• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is abstract class instance variable in servlet container - thread-safe

 
Amy Saari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I need some help if you have a minute. We have an app in production that's behaving poorly. We know we have a NOT thread safe condition where two threads running through the same servlet are looking at the same value in an instance variable (userName in the example below). So, for example, two threads in the class first are looking at a single copy of userName.

But what about this - can one thread in first and one thread in second be looking at the same instance of userName? if baseclass is abstract, then are there two instances of userName? one when new is called for class first and one when new is called for class second by the servlet container? know what i mean?

Here's some simple pseudo code to illustrate :




thanks for any thoughts,
amy
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65126
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to ask Struts questions in the Struts forum. I have moved this post there for you.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65126
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the class is a servlet there will be only one instance shared across all threads and there will be trouble. Write servlets in a thread-safe manner always.
 
Amy Saari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

I wish you had not moved this to Struts - it doesn't have anything to do with Struts (I should have just left the Action off and put in HttpServlet). Can you / would you move it back perhaps?

We know it's not thread safe. The question is this:

If two classes extend an abstract class that extends HttpServlet - how many instance of an instance variable in the abstract class exist? One or two?

Thanks,
Amy
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65126
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There will be one instance of the variable per instance of the class. The fact that it was declared in an abstract superclass is moot.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which version of Struts are you using where Action extends HttpServlet?
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amy,
Just to correct you on your thoughts,
ActionServet is the only action which is servlet among all the action classes struts provide.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic