简体   繁体   English

JSF2:使用EJB在视图中打开会话?

[英]JSF2: Open Session in View with EJBs?

Does it make sense to talk about the Open Session In View Pattern within JSF2 applications? 在JSF2应用程序中讨论视图模式中的Open Session是否有意义? My app has JSF2 Managed Beans calling Business Service EJBs that do all the db-related stuff (there's a DAO layer but that doesn't matter right now). 我的应用程序有JSF2 Managed Beans调用业务服务EJB来执行所有与db相关的东西(有一个DAO层,但现在无关紧要)。

Having OSIV pattern would mean that the Managed Bean would have to somehow make sure the underlying session was opened. 拥有OSIV模式意味着Managed Bean必须以某种方式确保底层会话被打开。

I am also using JPA. 我也在使用JPA。

Theoretically, the issue is exactly the same: entity will become detaches when they leave the EJB unless something keeps the scope of the EntityManager open. 从理论上讲,问题完全相同:实体在离开EJB时会变为分离,除非某些东西保持EntityManager的范围。 (Here is a great post about the topic in general: JPA implementation patterns: Lazy loading ). (这是关于该主题的一篇很棒的帖子: JPA实现模式:延迟加载 )。

From a blog post I read: 从我读过的博客文章中

8) No Open Entity Manager In View support. 8)没有Open Entity Manager In View支持。 [...] In EJB3, when your entity leaves bean with transaction scoped EntityManager, it is detached from persistence context and you may no longer rely on lazy loading (in fact, JPA specification does not specify the behavior in such situation, probably some vendor dependent exception will be thrown...) Of course, you may use EntityManager with extended persistence context , holding the transaction and persistence context as long as you want. [...]在EJB3中,当您的实体使用事务范围的 EntityManager离开bean时,它与持久性上下文分离,您可能不再依赖于延迟加载(事实上,JPA规范没有指定在这种情况下的行为,可能是某些将抛出与供应商相关的异常...)当然,您可以将EntityManager与扩展持久化上下文一起使用,只要您愿意,就可以保留事务和持久性上下文。 But this feature is only available for SFSB, while DAO classes are typical examples of stateless services, since they only dispatch calls to the persistence layer. 但是此功能仅适用于SFSB,而DAO类是无状态服务的典型示例,因为它们仅将调用分派给持久层。 Additionally, having dedicated DAO bean instance for each client seems to be a big overkill. 此外,为每个客户端提供专用的DAO bean实例似乎是一个很大的过度杀伤力。

I'm however not sure it is really true. 但我不确定这是真的。 From my understanding you should be able to write a servlet filter which uses the UserTransaction to start and commit the transaction (like the regular filter in OSIV). 根据我的理解,你应该能够编写一个servlet过滤器,它使用UserTransaction来启动和提交事务(就像OSIV中的常规过滤器一样)。 EJB would then participate in the transaction started in the filter and the EntityManager would remain open. 然后,EJB将参与在过滤器中启动的事务,并且EntityManager将保持打开状态。 I haven't tested it though, but my suggestion would be to give it a try. 我没有测试过,但我的建议是尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM