Spring Request scope vs java thread-local

前端 未结 3 789
轮回少年
轮回少年 2021-01-31 04:57

In high volume (~50,000 requests per second) java web-app I\'m using ThreadLocal to execute a task which should be executed per request scope.

I could achieve the same e

3条回答
  •  温柔的废话
    2021-01-31 05:06

    Regarding the ThreadLocal solution I would like to add that there is probably a thread pool in your web server (for ex: Tomcat) and your thread local variable won't actually be cleared upon completion of each request as processing threads don't die with a thread pool enabled.

    You need to clear the thread-local variable (threadLocal.remove()) manually upon complection of each request. For that you can use, for example, some kind of afterCompletion() of some of those Spring request/response interceptors.

提交回复
热议问题