JavaEE6 DAO:应该是@Stateless 还是@ApplicationScoped? [英] JavaEE6 DAO: Should it be @Stateless or @ApplicationScoped?

查看:20
本文介绍了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:

  1. EJB 由容器池化,因此如果每个池有 N 个实例和数千个表,您只会浪费资源(更不用说部署时的成本了).

  1. 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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