ThreadLocal是否优于HttpServletRequest.setAttribute(" key"," value")? [英] Is ThreadLocal preferable to HttpServletRequest.setAttribute("key", "value")?

查看:101
本文介绍了ThreadLocal是否优于HttpServletRequest.setAttribute(" key"," value")?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

servlet规范(参见我之前的问题)保证相同的线程将执行所有过滤器和关联的Servlet。鉴于此,如果可以选择使用 ThreadLocal HttpServletRequest.setAttribute 传递数据没有任何用处>(假设您正确清理)。我觉得使用 ThreadLocal 有两个好处:类型安全性和更好的性能,因为没有使用字符串键或映射(除了可能进入线程集合之外)字符串)线程ID)。

The servlet spec (see my previous question) guarantees that the same thread will execute all Filters and the associated Servlet. Given this, I do not see any use for passing data using HttpServletRequest.setAttribute if there is the option to use a ThreadLocal (assuming you clean up properly). I feel that there are two benefits to using ThreadLocal: type-safety and better performance because no string keys or maps are being used (except probably into a thread collection by (non-string) thread id).

有人可以确认我是否正确所以我可以继续放弃 setAttribute

Could someone please confirm if I am right so I can proceed with abandoning setAttribute?

推荐答案


ThreadLocal优于HttpServletRequest.setAttribute(key,value )?

取决于具体的功能要求。

Depends on the concrete functional requirement.

例如,JSF将 FacesContext 存储在 ThreadLocal 。这使您可以访问所有JSF工件,包括代码中任何位置的原始 HttpServletRequest HttpServletResponse FacesServlet 执行,例如托管bean。大多数其他基于Java的MVC框架都遵循相同的示例。

JSF, for example, stores the FacesContext in a ThreadLocal. This enables you to access all of the JSF artifacts, including the "raw" HttpServletRequest and HttpServletResponse anywhere in the code which is executed by the FacesServlet, such as managed beans. Most other Java based MVC frameworks follow the same example.

根据您的评论,


我主要需要将User和EntityManager对象从用户和数据库过滤器传输到Servlet。我还发现在代码中经常出乎意料地需要这些代码,我很想在Servlet之外使用它们(即在doGet调用的嵌套代码中)。我觉得有更深层次的代码可能有更好的方法 - 建议?

至于用户我假设这是一个会话属性的例子,我宁愿采用与JSF相同的方法。创建 ThreadLocal< Context> 其中 Context 是您的自定义包装类,其中包含对当前 HttpServletRequest ,也许还有 HttpServletResponse ,以便您可以在代码中的任何位置访问它们。如果有必要,可以直接从 Context 类中提供方便的方法来获取用户

As to the User example for which I assume that this is a session attribute, I'd rather follow the same approach as JSF. Create a ThreadLocal<Context> where the Context is your custom wrapper class holding references to the current HttpServletRequest and maybe also HttpServletResponse so that you can access them anywhere down in your code. If necessary provide convenience methods to get among others the User directly from the Context class.

至于 EntityManager 示例,你可以采用相同的方法,但我个人不会把它放在相同的 ThreadLocal< Context> ,而是另一个。或者,更好的是,只需从服务层中的JNDI获取它,这将允许您对事务进行更细粒度的控制。无论如何,请确保您正确处理提交/关闭。从容器中接管持久性和事务管理应该非常小心。我真的重新考虑使用现有的和设计良好的API /框架(如EJB / JPA)的厌恶,否则你将冒着浪费时间重新发明所有已经标准化的API和东西的风险。

As to the EntityManager example, you could follow the same approach, but I'd personally not put it in the same ThreadLocal<Context>, but rather a different one. Or, better, just obtain it from JNDI in the service layer, which would allow you more finer grained control over transactions. In any case, please make absolutely sure that you handle the commit/close properly. Taking over the persistence and transaction management from the container should be done with extreme care. I'd really reconsider the aversion against using the existing and well-designed APIs/frameworks like EJB/JPA, otherwise you'll risk a complete waste of time of reinventing all the already-standardized APIs and stuffs.

  • Retrieving Web Session from a POJO Outside the Web Container
  • Design Patterns web based applications

这篇关于ThreadLocal是否优于HttpServletRequest.setAttribute(&quot; key&quot;,&quot; value&quot;)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆