Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing thread-safe Actions

 
Eugene Abarquez
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • 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
 
Alaa Nassef
Ranch Hand
Posts: 471
Hibernate Mac OS X Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic