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

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

我目前正在创建一个 EJB3 数据访问类来处理我的 Java EE 6 应用程序中的所有数据库操作.现在,由于 Java EE 6 提供了新的 ApplicationScoped 注释,我想知道我的 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 会话 Bean 还是 @ApplicationScoped Bean 会更好吗?@Singleton 怎么样?这些与 DAO 相关的选项之间有什么区别?

Would 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?

我正在使用带有完整 Java EE 6 平台的 Glassfish 3.0.1

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?

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

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.

    EJB 3.1 中引入的 @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:06,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1598082.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:应该是   DAO   Stateless   ApplicationScoped

    发布评论

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

    >www.elefans.com

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