Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8117981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:20:22+00:00 2026-06-06T04:20:22+00:00

Background The Apache Action class is not thread-safe. However, this was only realized after

  • 0

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:

  1. Create an instance of the Action subclass.
  2. Re-use that same instance for every request.
  3. Obtain a thread (from a thread pool) when receiving a new connection.
  4. Call execute on the Action subclass from the thread.
  5. 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:20:24+00:00Added an answer on June 6, 2026 at 4:20 am

    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 call super.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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The two paragraphs after this are background information in case I'm thinking of this
Background I admit, this question stems from an ultimate lack of deep understanding of
Background: I have this with rollup query defined in MySQL: SELECT case TRIM(company) when
Background: I'm writing a 'standard' (nothing special) web application in Ruby (not Rails) and
Background We need to develop a specialised CMS (internal use only) to support a
Some background: I setup six blogs this week, all using Wordpress 2.92, installed with
Background There are many tools to analyze logfiles from servers like Apache HTTP server,
I come from a linux/apache/php/mysql background. For my current project, I am forced to
Quick background. CFHTTP doesn't support Windows NTLM/Authenticate authentication, only basic authentication. I need to
Hi pardon my ignorance i'm coming from an apache background. On IIS/ASP.NET (.NET 4.0)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.