我正在创建一个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?
发布评论