• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Writing thread-safe Actions

 
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a question about writing thread-safe Actions.

I just recently discovered that Struts creates only one instance of the Action object for multiple sessions, and having a global variable in your Action objects makes your code not thread-safe. This was after I realized that my application was behaving quite differently when multiple users are using the application, and the pages are messed up.

So I am thinking of making some code refactoring to make the code thread-safe. The structure of my code is simple: I have a BaseAction class from which the other Action classes are extended. Here's the current code:





I already removed some of the global variables that used to be in the BaseAction class, but the application is still behaving differently in multiple sessions.

So my first question is: Is the current structure of my code not thread-safe? And, how can I refactor the code to make it thread-safe?

Thanks in advance,

Eugene
 
Ranch Hand
Posts: 471
Mac OS X Hibernate Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Eugene,

I do believe that your code is totally not thread safe, and the reason is that you are using instance variables for request, response, mapping & form. You should ditch the execute method that you have in the BaseAction class completely, and override the execute method in all your extended actions. If you need to have a common behavior to be done before executing your action, you can simply put a method in the BaseAction that has this behavior, and call it from the extended actions.
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep�I agree with the response by Alaa�having instance variables in your action class are a no-no with Struts. You can pass stuff around as parameters (like Struts does). You can have final instance variables, but that can only be used for stuff that would not change between instances. For example you might have a final logger class, but stuff like the form and request should not be stored in instance variables.

If you are trying to clean up the method signatures of your base class, you can wrap the parameter in a class that you pass around. For example this example uses a class named ActionParams (which is a class you would have to write):


- Brent
 
Eugene Abarquez
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Guys,

Thanks for your inputs. At first, I was suspecting that those instance variables were the culprit, but it turns out that my problem was caused by a different bug. However I followed your suggestion and I removed all those instance variables from the BaseAction class entirely.

Thank you again.

Eugene
 
reply
    Bookmark Topic Watch Topic
  • New Topic