JEE7:EJB 和 CDI bean 是否支持容器管理的事务? [英] JEE7: Do EJB and CDI beans support container-managed transactions?

查看:19
本文介绍了JEE7:EJB 和 CDI bean 是否支持容器管理的事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java EE7 由一堆bean"定义组成:

Java EE7 consists of a bunch of "bean" definitions:

  • 托管 Bean 1.0 (JSR-316/JSR-250)
  • Java 1.0 的依赖注入 (JSR-330)
  • CDI 1.1 (JSR-346)
  • JSF 托管 Bean 2.2 (JSR-344)
  • EJB 3.2 (JSR-345)

为了摆脱脑海中的混乱,我研究了几篇何时使用哪种bean类型"的文章.EJB 的优点之一似乎是它们单独支持声明性容器管理事务(著名的事务注释).不过,我不确定这是否正确.任何人都可以批准吗?

In order to get rid of the chaos in my mind, I studies several articles of "when to use which bean type". One of the pros for EJB seems to be that they alone support declarative container-managed transactions (the famous transaction annotations). I'm not sure, though, if this is correct. Can anyone approve this?

与此同时,我想出了一个简单的演示应用程序来检查这是否属实.我刚刚根据 这个 片段:

Meanwhile, I came up with a simple demo application to check if this was actually true. I just defined a CDI bean (not an EJB - it has no class level annotations) as follows, based on this snippet:

public class CdiBean {
    @Resource
    TransactionSynchronizationRegistry tsr;

    @Transactional(Transactional.TxType.REQUIRED)
    public boolean isTransactional() {
        return tsr.getTransactionStatus() == Status.STATUS_ACTIVE;
    }
}

现在,GlassFish 4.0 的结果是该方法实际上返回 true,根据我的查询,它没有按预期工作.我确实希望容器忽略 CDI bean 方法上的 @Transactional 注释,甚至抛出异常.我使用的是新安装的 GlassFish 4 服务器,所以没有干扰.

Now, the outcome on GlassFish 4.0 is that this method actually returns true, which, according to my inquiries, is not working as expected. I did expect the container to ignore the @Transactional annotation on a CDI bean method, or to even throw an exception. I use a freshly-installed GlassFish 4 server, so there are no interferences.

所以我的问题是:

  • 哪些 bean 类型确实支持容器管理的事务?
  • 出于好奇,如果上面的代码是错误的,我如何用一个简单的演示应用程序来测试它?

(顺便说一句:有人在此处描述了类似的问题,但其解决方案不适用于我的情况.

(BTW: Someone described a similar problem here, but its solution does not apply to my case.

推荐答案

直到 Java EE 7 只有 EJB 是事务性的,并且 @Transactional 注释不存在.

Until Java EE 7 only EJB was transactional and the @Transactional annotation didn't exist.

从 Java EE 7 和 JTA 1.2 开始,您可以在 CDI 中使用带有 @Transactional 注释的事务拦截器.

Since Java EE 7 and JTA 1.2 you can use transactional interceptor in CDI with @Transactional annotation.

要回答有关要使用的最佳 bean 类型的问题,默认情况下答案是 CDI.

To answer your question about the best type of bean to use, the answer is CDI by default.

CDI bean 比 EJB 更轻,支持很多特性(包括作为 EJB)并且默认激活(当你将 beans.xml 文件添加到你的应用程序时).由于 Java EE 6 @Inject 取代了 @EJB.即使您使用远程 EJB(CDI 中不存在的功能),最佳实践建议您@EJB 一次注入远程 EJB 和 CDI 生产者以将其公开为 CDI bean

CDI beans are lighter than EJB and support a lot of feature (including being an EJB) and is activated by default (when you add beans.xml file to your app). Since Java EE 6 @Inject supersede @EJB. Even if you use remote EJBs (feature not existing in CDI) the best practice suggest that you @EJB once to inject remote EJB and a CDI producer to expose it as a CDI bean

public class Resources {

    @EJB
    @Produces
    MyRemoteEJB ejb;

}

同样建议 Java EE 资源

The same is suggested for Java EE resources

public class Resources2 {

    @PersistenceContext
    @Produces
    EntityManager em;

}

这些生产者以后会用到

public class MyBean {

    @Inject
    MyRemoteEJB bean;

    @Inject
    EntityManager em;

}

EJB 对它们包含的某些服务(如 JMS 或异步处理)仍然有意义,但您会将它们用作 CDI bean.

EJB continue to make sense for certain services they include like JMS or Asynchronous treatment, but you'll use them as CDI bean.

这篇关于JEE7:EJB 和 CDI bean 是否支持容器管理的事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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