Background
The Apache Action class is not thread-safe. However, this was only realized after implementing a base class, upon which all other classes in the system depend. The base class uses a number of instance variables:
private HttpServletRequest request;
private ArrayList inputParams = new ArrayList();
private Connection connection;
private String outputParameter;
private boolean exportEnabled;
Fortunately, all usages of those variables are accomplished through accessor methods, exclusively. For example:
public boolean getExportEnabled() {
return this.exportEnabled;
}
public void setExportEnabled( boolean exportEnabled ) {
this.exportEnabled = exportEnabled;
}
Problem
The base class is running in a multi-threaded Servlet environment.
Solution #1
To resolve this issue, I was thinking about using a HashMap keyed to the session. However, this would require re-writing all of the methods, and dependent code:
private static HashMap sessionVariables = new HashMap();
public boolean getExportEnabled( HttpSession session ) {
return getSessionVariables().get( session.getId() + '.exportEnabled' );
}
public void setExportEnabled( boolean exportEnabled, HttpSession session ) {
getSessionVariables().put( session.getId() + '.exportEnabled', exportEnabled );
}
That is a lot of work, and would likely introduce bugs.
Solution #2
It might be possible to change the base class to an “empty” class. This empty class would have a single method:
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response )
throws Exception {
// Instantiate "this" and forward the request?
}
But it would have to know the appropriate base class to instantiate, or perhaps instantiate a new version of itself to handle the call.
Update #1
I believe the Struts architecture does the following:
- Create an instance of the Action subclass.
- Re-use that same instance for every request.
- Obtain a thread (from a thread pool) when receiving a new connection.
- Call
executeon the Action subclass from the thread. - Handle multiple new connections using different threads.
The same execute method will be called on the same instance of the object, resulting in unsafe behaviour because the subclass has instance variables.
Update #2
The following solution seems to solve the issue:
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response ) throws Exception {
((MyClass)clone()).executeClone( mapping, form, request, response );
}
public ActionForward executeClone(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response ) throws Exception {
// Former "execute" method code goes here.
// ...
}
The original execute method was renamed to executeClone. The new execute implementation creates a clone of the current class and subsequently calls executeClone. This minimally invasive technique avoids introducing new bugs while making the class thread-safe.
Question
What would be the most reliable way to make the code thread-safe while minimizing the risk of introducing bugs?
Thank you!
Solution #1 is dangerous because it assumes that the session is thread-safe, which is not necessarily the case. Someone could be making two simultaneous requests with the same session.
Solution #2 could be easily implemented by making your base class implement
Cloneable. Then it can clone itself, set the clone’s instance variables, and then callsuper.execute(). If you think it’s too hard to change the design to make your base class be properly thread-safe, this might be an easy way out.