Accessing HttpSession outside of the originally receiving thread

天大地大妈咪最大 提交于 2019-12-01 18:23:41

The HttpSession object itself can be used in multiple threads (but is not thread-safe and therefore must be synchronized). However Spring is doing some extra magic e.g. when you have session-scoped beans. Namely it uses ThreadLocal underneath to bind current session with thread.

I don't know what is your exact scenario, but apparently Spring tries to retrieve HttpSession from this ThreadLocal while you are in another thread - which obviously fails.

The solution is simple - extract session attributes you need in @Async method and pass them directly. This is by the way much better design - avoid passing HttpSession object around because it makes testing harder and your code is much less likely to be reused in the future.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!