How Playframework 1.x static method works in a thr

2019-08-08 06:21发布

问题:

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 ThreadLocals 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 Strings. 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!