title: Thread safety in Java web application data access class
body: <p>A hobby project of mine is a Java web application.  It's a simple web page with a form.  The user fills out the form, submits, and is presented with some results.</p>
  
  <p>The data is coming over a JDBC Connection.  When the user submits, I validate the input, build a "CREATE ALIAS" statement, a "SELECT" statement, and a "DROP ALIAS" statement.  I execute them and do whatever I need to do with the ResultSet from the query.</p>
  
  <p>Due to an issue with the ALIASes on the particular database/JDBC combination I'm using, it's required that each time the query is run, these ALIASes are created with a unique name.  I'm using an int to ensure this which gets incremented each and every time we go to the database.</p>
  
  <p>So, my data access class looks a bit like:</p>
  
  <pre><code>private final static Connection connection = // initialized however
  
  private static int uniqueInvocationNumber = 0;
  
  public static Whatever getData(ValidatedQuery validatedQuery) {
      String aliasName = "TEMPALIAS" + String.valueOf(uniqueInvocationNumber);
      // build statements, execute statements, deal with results
      uniqueInvocationNumber++;
  }
  </code></pre>
  
  <p>This works.  However, I've recently been made aware that I'm firmly stuck in Jon Skeet's phase 0 of <a href="https://stackoverflow.com/questions/423823/whats-your-favorite-programmer-ignorance-pet-peeve/424160#424160">threading knowledge</a> ("Complete ignorance - ignore any possibility of problems.") - I've never written either threaded code or thread-aware code.  I have absolutely no idea what can happen when many users are using the application at the same time.</p>
  
  <p>So my question is, (assuming I haven't stumbled to thread-safety by blind luck / J2EE magic):</p>
  
  <p>How can I make this safe?</p>
  
  <p>I've included information here which I believe is relevant but let me know if it's not sufficient.</p>
  
  <p>Thanks a million.</p>
  
  <p><strong>EDIT:</strong> This is a proper J2EE web application using the Wicket framework.  I'm typically deploying it inside Jetty.</p>
  
  <p><strong>EDIT:</strong> A long story about the motivation for the ALIASes, for those interested:</p>
  
  <p>The database in question is DB2 on AS400 (i5, System i, iSeries, whatever IBM are calling it these days) and I'm using <a href="http://jt400.sourceforge.net/" rel="nofollow noreferrer">jt400</a>.</p>
  
  <p>Although DB2 on AS400 is kind of like DB2 on any other platform, tables have a concept of a "member" because of legacy stuff.  A member is kind of like a chunk of a table.  The query I want to run is</p>
  
  <pre><code>SELECT thisField FROM thisTable(thisMember)
  </code></pre>
  
  <p>which treats thisMember as a table in its own right so just gives you thisField for all the rows in the member.</p>
  
  <p>Now, queries such as this run fine in an interactive SQL session, but don't work over JDBC (I don't know why).  The workaround I use is to do something like</p>
  
  <pre><code>CREATE ALIAS tempAlias FOR thisTable(thisMember)
  </code></pre>
  
  <p>then a </p>
  
  <pre><code>SELECT thisField FROM tempAlias
  </code></pre>
  
  <p>then a </p>
  
  <pre><code>DROP ALIAS tempAlias
  </code></pre>
  
  <p>which works but for one show-stopping issue:  when you do this repeatedly with the ALIAS always called "tempAlias", and have a case where thisField has a different length from one query to the next, the result set comes back garbled for the second query (getString for the first row is fine, the next one has a certain number of spaces prepended, the next one the same number of spaces further prepended - this is from memory, but it's something like that).</p>
  
  <p>Hence the workaround of ensuring each ALIAS has a distinct name which clears this up.</p>
  
  <p>I've just realised (having spent the time to tap this explanation out) that I probably didn't spend enough time thinking about the issue in the first place before seizing on the workaround.  Unfortunately I haven't yet fulfilled my dream of getting an AS400 for my bedroom ;) so I can't try anything new now.</p>