ThreadLocal in Play

ThreadLocal in Play

Developers with Java experience may have previously used ThreadLocal to store/read data isolated to a specific system user or user request. One common example is working with legacy servlet containers when each user request was allocated a single thread for an entire request/response roundtrip.

ThreadLocal's have never been a very good solution for any JVM application - they pass parameters without actually passing parameters to save on a little bit of boiler plate. They make code harder to test, since the ThreadLocal context must be set up. They also undermine the type system, since they make it impossible to know whether its safe to invoke a method or not, since the signature of that method does not say what ThreadLocals it takes.

ThreadLocals don't fit with Reactive Programming and Play is built on this model. The reason for this is that Reactive application code is asynchronous and hops around between lots of different threads. There is no guarantee which thread portions of your code will run in. For example, in Play the thread that accepts a request may not be the same thread that makes calls to external services or serves the corresponding response. Java 8 in general has the same problem, for example using the Java 8 streams API, which does its operations in parallel on other threads, cannot be used safely with code that depends on ThreadLocals.

Fortunately Play has better functionality to help you store data that is isolated and available to a specific request, session, or for the next user request only (Flash). Here are a few helpful links:

    • Related Articles

    • Play Authentication/Authorization

      The Play documentation pages for Action Composition outline how to use the basic authentication functionality included with Play or build your own functionality. One possible solution is to use OAuth as documented within the Play website. For ...
    • Play Performance Tuning

      General performance tuning advice Run experiments to tune your application. It’s not reliable to rely on assumptions. To tune your application you need to be systematic. Make hypotheses, control variables and test, test, test! It can be a good idea ...
    • How to monitor my Play application? Can I use Cinnamon?

      Yes! Starting in version 2.7 Cinnamon added support for monitoring Play 2.6+ applications. For more information, you can check out the announcement here.
    • Can Lagom errors be handled using Play error handling?

      No, the Play error handler is not invoked from Lagom, because the Lagom exception serializer intercepts it first. Any exception thrown by application code will be wrapped in a transport exception, serialized to json and a response is built from it. ...
    • Why can't I see Cinnamon metrics when running my Play application in development?

      A common cause for Cinnamon metrics not being reported in Play development is because Cinnamon does not currently support Play’s development mode*, which is the mode used by the Play server when started using the sbt run command. As a workaround, ...