问题
Might be a silly question, but I'm noob learning Play framework 1.2.5. I've noticed that the controller actions are all static methods and I assume each request will be processed in a separate thread. If that's the case, how Play framework ensures these static controller methods are thread safe? I think, as long as we are not sharing anything between controller methods then we are good (?), please correct me if I'm wrong.
Thanks in advance.
回答1:
Yes we're quite good. As you know HTTP is stateless, so nothing will be shared between requests. Play uses ThreadLocal
s while processiong the request. Each request has it's own thread so you have full control of the current request - response life cycle.
If you want to share data between controllers, you have session
object however it's quite small and accepts only String
s. For other data types or different implementations of sharing, you should use a Cache
implementation. For further information see session and cache documentations.
Good luck!
来源:https://stackoverflow.com/questions/16110757/how-playframework-1-x-static-method-works-in-a-thread-safe-way