Accessing HttpSession outside of the originally receiving thread

前端 未结 1 634
轻奢々
轻奢々 2021-01-19 17:55

I\'m using Spring 3. When controller gets requests it passes control to method someMethod() annotated with @Async in Service bean and then returns.

相关标签:
1条回答
  • 2021-01-19 18:17

    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.

    0 讨论(0)
提交回复
热议问题