• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Struts 1.2 session problem

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi folks

I've come across session interchanging issue with struts 1.2 action classes. When i checked in to i could identify that session attribute is defined globally for that action class. I understand unlike struts 2.0 this version generates threaded architecture which is somewhat similar to how servlet works. Still I can not think of how could this be a problem or what is the relationship in different threads for globally defined and lazily initialized http session where it uses in several action types.

I resolved the problem by defining that inside the action methods. Also note this happens when I execute that action type from two different browsers at a same second. I tried with IE7 and FF3.5 still the results is same (from two different machines as well).

Please clarify this. Thanks in advance.

Charith de Silva
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know of any session issues in Struts 1.2 if you've coded your actions correctly.

Could you be more specific as to how your actions are coded and why you think the problem lies with Struts?
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From your post, it looks like you've problem with the struts session object when you defined them as action instance variable and this problem gets solved when you create and use them locally, well if that's the case, then never ever define and use the instance variable and as you know the reason that action classes are servlets.
 
Charith De Silva
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your prompt replies.

@David - Its basically nothing much different than sample action other than globally defined attritute for session. I'm remotely browsing so that i dnt hv access to my actual code. Basically it would be as follows.




@Sagar
I'm confused why session can not be used as action class attributes.




Thanks
Charith
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Charith De Silva wrote:@Sagar
I'm confused why session can not be used as action class attributes.


Because Struts actions are Servlets and Servlets uses thread to process multiple simultaneous request and instance variables will be shared across all threads.

Google servlet instance variable + thread safe for more info.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Struts actions *aren't* servlets, but still must be thread-safe--there is only one action per mapping, like servlets.
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, Struts action are not Servlets, but they works like Servlets and follow the servlets thread safety guidelines.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Struts Actions are NOT Thread Safe.
Do not define any instance level variables in Action Classes for this reason.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

James Ward wrote:Struts Actions are NOT Thread Safe.
Do not define any instance level variables in Action Classes for this reason.


Yeah, that's what we said.

That said, it's fine to have instance variables--as long as you're aware they're shared and code accordingly. (Or ThreadLocal, I suppose.)
 
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!