JavaEE6 DAO:应该是@Stateless还是@ApplicationScoped?

编程入门 行业动态 更新时间:2024-10-21 17:32:04
本文介绍了JavaEE6 DAO:应该是@Stateless还是@ApplicationScoped?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在创建一个EJB3数据访问类来处理我的Java EE 6应用程序中的所有数据库操作。现在,由于Java EE 6提供了新的ApplicationScoped-Annotation,我想知道我的EJB应该有什么状态,或者它应该是无状态的。

I'm currently creating an EJB3 Data Access Class to handle all database operations in my Java EE 6-application. Now, since Java EE 6 provides the new ApplicationScoped-Annotation, I wonder what state my EJB should have, or if it should be stateless.

最好让DAO成为@Stateless Session Bean,还是@ApplicationScoped Bean? @Singleton怎么样?这些与DAO有关的选项有什么区别?

Whould it be better to let the DAO be a @Stateless Session Bean, or an @ApplicationScoped Bean? What about @Singleton? What are the differences between these options related to a DAO?

编辑: 我正在使用Glassfish 3.0.1与完整的Java EE 6平台

I'm using Glassfish 3.0.1 with the full Java EE 6 platform

推荐答案

最好让DAO成为一个@Stateless Session Bean,还是一个@ApplicationScoped Bean? @Singleton怎么样?这些与DAO有关的选项有什么区别?

Whould it be better to let the DAO be a @Stateless Session Bean, or an @ApplicationScoped Bean? What about @Singleton? What are the differences between these options related to a DAO?

我不会使用无状态会话Bean作为DAO:

I would NOT use Stateless Session Beans for DAOs:

  • EJB由容器集合,所以如果每个池有N个实例和数千个表,那么您只是浪费资源(甚至在部署时甚至不要说成本)。

  • EJBs are pooled by the container so if you have N instances per pool and thousands of tables, you're just going to waste resources (not even to mention the cost at deploy time).

    实现DAO作为SLSB将鼓励EJB链接,这不是一个很好的做法,从可扩展性的角度来看。

    Implementing DAOs as SLSB would encourage EJB chaining which is not a good practice from a scalability point of view.

    我不会将DAO图层绑定到EJB API。

    I would not tie the DAO layer to the EJB API.

    @Singleton 可以使事情有所改善,但是我仍然不会将DAO实现为EJB。我宁愿使用CDI(也可能是自定义的刻板印象,请参阅这篇文章)。

    The @Singleton introduced in EJB 3.1 could make things a bit better but I would still not implement DAOs as EJBs. I would rather use CDI (and maybe a custom stereotype, see this article for example).

    或者我根本不会使用DAO。 JPA的实体经理是域名商店模式的实现,并将对域商店的访问包装在一个DAO没有多少价值。

    Or I wouldn't use DAOs at all. JPA's entity manager is an implementation of the Domain Store pattern and wrapping access to a Domain Store in a DAO doesn't add much value.

  • 更多推荐

    JavaEE6 DAO:应该是@Stateless还是@ApplicationScoped?

    本文发布于:2023-11-15 21:25:22,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1598085.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:应该是   DAO   Stateless   ApplicationScoped

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!