ThreadLocal Singleton

Pieter-Jan picture Pieter-Jan · Feb 19, 2013 · Viewed 9.6k times · Source

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:

Situation:

  1. User1 logs on and database path is set to 'db/user1/'
  2. User1 inserts 'Value 1' into the database
  3. User2 logs on and database path is set to 'db/user2/'
  4. User1 tries to delete 'Value 1' from database

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.

public class DataAccess{
    private static DataAccess dataaccess;
    private String databasepath;

    public static DataAccess getInstance() {
        if (dataaccess == null) {
            dataaccess = new DataAccess();
        }
    }
}

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?

Any help is most certainly appreciated.

Answer

Boris the Spider picture Boris the Spider · Feb 19, 2013

You could use the ThreadLocal class in a factory pattern:

public class DataAccess{
    private static ThreadLocal<DataAccess> THREAD_LOCAL = new ThreadLocal() {
     @Override
     protected DataAccess initialValue() {
             return new DataAccess();
     }
    };
    private String databasepath;

    public static DataAccess getInstance() {
      return THREAD_LOCAL.get();
    }
}

This will, however, cause a memory leak. So you need to use a Servlet Filter to set the value at the start of a request and then delete it at the end, some like:

   public void doFilter(ServletRequest request,
      ServletResponse response, FilterChain chain) 
      throws IOException, ServletException {
      DataAccess.set(new DataAccess("SomeValue"));
      try {
        chain.doFilter(request, response);
      } finally {
        DataAcess.remove();
      }
   }

Once you have a class that implements Filter you add it to your web.xml thus:

<!--Filter for adding in dataccess objects-->
<filter>
    <filter-name>DataccessFilter</filter-name>
    <filter-class>my.package.DataccessFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>DataccessFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

This page gives an example of a filter and its mappings.
And your DataAccess would look like:

public class DataAccess{
    private static ThreadLocal<DataAccess> THREAD_LOCAL = new ThreadLocal();
    private String databasepath;

    public DataAcess(final String databasepath) {
      this.databasepath = databasepath;
    }

    public static DataAccess getInstance() {
      return THREAD_LOCAL.get();
    }
    public static void set(final DataAccess dataAccess) {
      THREAD_LOCAL.set(dataAccess);
    }
    public static void remove() {
      THREAD_LOCAL.remove();
    }
}

Be extremely careful with ThreadLocal as it is probably the number one cause of memory leaks in Java. With web servers that have thread pools you can get even more serious bugs if you don't clean them out properly.