title: ThreadLocal Singleton
body: <p>I'm running a RESTful java backend on GlassFish. Attached to that is an HTML5 / JS frontend which I can put in a webapp project (and then include the backend as a dependency), or run on an IIS webserver on a different location. CORS is not an issue. Whatever solves this following problem:</p>
  
  <p>Situation:</p>
  
  <ol>
  <li>User1 logs on and database path is set to 'db/user1/'</li>
  <li>User1 inserts 'Value 1' into the database</li>
  <li>User2 logs on and database path is set to 'db/user2/'</li>
  <li>User1 tries to delete 'Value 1' from database</li>
  </ol>
  
  <p>User1 would not be able to delete Value 1 from db/user1, since the databasepath has been changed to db/user2 and there is no Value 1 in that database.</p>
  
  <pre><code>public class DataAccess{
      private static DataAccess dataaccess;
      private String databasepath;
  
      public static DataAccess getInstance() {
          if (dataaccess == null) {
              dataaccess = new DataAccess();
          }
      }
  }
  </code></pre>
  
  <p>How can I modify the getInstance() method so that it acts as a singleton, but only inside the thread of that user? I saw something called threadlocal but didn't fully understand it, is that perhaps a solution?</p>
  
  <p>Any help is most certainly appreciated. </p>