8

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.

Pieter-Jan
  • 1,675
  • 2
  • 19
  • 25

2 Answers2

11

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.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • 2
    Notice that `ThreadLocal`s don't work if you use anything special, e.g. asynchronous EJB's, Comets/Continuations, because they will replace the running Thread with another one. – gaborsch Feb 19 '13 at 14:10
  • Thanks. That's what I'm looking for. One more question... I'm still unsure where that doFilter goes... Do I just make a new class with the content above and then apply it in my Jersey's web.xml ? – Pieter-Jan Feb 19 '13 at 14:31
  • You could add an example for a `Filter` in `web.xml`, and that will be the perfect complete answer. – gaborsch Feb 19 '13 at 14:35
  • @Pieter-Jan Exactly. The filter order does not matter in this case (unless you use the `DataAccess` in some of your filters). – gaborsch Feb 19 '13 at 14:41
  • I'm getting there. Thanks for all the effort so far. I'm still getting an error on that wrapper in the filter, which makes sense cause it isn't defined anywhere.. – Pieter-Jan Feb 20 '13 at 09:33
0

Looks that ThreadLocal should help you with your use-case:

http://docs.oracle.com/javase/6/docs/api/java/lang/ThreadLocal.html

This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable.

Raman
  • 887
  • 4
  • 12
  • 28