bean 的 CDI @TransactionAttribute

发布于 2024-11-27 05:29:48 字数 1336 浏览 2 评论 0原文

我正在测试应用程序上尝试使用CDI。我有一个 DAO,它注入一个容器管理的 JTA 持久化上下文,如下所示:

public class TestDAO implements Serializable {
    @PersistenceContext
    private EntityManager entityManager;

    public void insertEntity(Test test) {
        entityManager.persist(test);
    }
}

现在我有一个如下所示的 CDI 控制器 bean:

@Named
@SessionScoped
public class TestController implements Serializable {
    @Inject
    private TestDAO testDAO;

    public void finishGame() {
        testDAO.insertEntity(new Test(1, 2, 3));
    }
}

如果我运行它,我会收到一个错误DAO 尝试插入实体时,因为没有可用的活动事务。到目前为止,一切都很好。我可以通过将控制器 bean 设为有状态 EJB 来解决此问题,它将把 finishGame() 包装在事务中。

但假设我不需要 EJB。作为测试,我使用 @TransactionAttribute 注释对 finishGame() 进行了注释,并且它起作用了(控制器 bean 不是 EJB)。所以我的问题是:它是如何工作的? CDI 是否为普通 bean 定义了 @TransactionAttribute ?我知道 Seam Persistence Module 可以做到这一点,但我没有使用它。实际上,我将其添加到项目中,但后来将其删除,因为我收到了尴尬的异常。

有人可以消除我的困惑吗? CDI 真的为普通 bean 定义了 @TransactionAttribute 吗?

PS我还有另一个问题。我发现趋势是将所有 EJB 注释移植到普通 bean。那么EJB将来会过时吗?我的意思是我在JIRA中看到@TransactionAttribute将来将为普通bean添加(任务仍然没有解决)。那么,这不是让 EJB 黯然失色、功能重复吗?

此致, 佩塔尔

I am experimenting with CDI on a test application. I have a DAO which injects a container managed JTA persistence context like this:

public class TestDAO implements Serializable {
    @PersistenceContext
    private EntityManager entityManager;

    public void insertEntity(Test test) {
        entityManager.persist(test);
    }
}

Now I have a CDI controller bean like this:

@Named
@SessionScoped
public class TestController implements Serializable {
    @Inject
    private TestDAO testDAO;

    public void finishGame() {
        testDAO.insertEntity(new Test(1, 2, 3));
    }
}

If I run this, I receive an error in the DAO when trying to insert the entity, because there is no active transaction available. So far so good. I can solve this by making the controller bean a stateful EJB which will wrap the finishGame() in a transaction.

But let assume I don't want an EJB. As a test I annotated the finishGame() with the @TransactionAttribute annotation and it worked(the controller bean is NOT an EJB). So my question is: how does it work? Does the CDI define @TransactionAttribute for plain beans? I know that Seam Persistence Module does this, but I am not using it. Actually I added it to the project, but I removed it after, because I received awkward exceptions.

Could anyone clear my confusion? Do really CDI define @TransactionAttribute for plain beans?

P.S. I have another sort of question. I see the tendencies is to port all EJB annotations to plain beans. So will EJBs become obsolete in the future? I mean I saw in JIRA that @TransactionAttribute will be added in the future for plain beans(the task is still not resolved). So isn't this eclipsing EJBs, sort of duplicating functionality?

Best regards,
Petar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许你一世情深 2024-12-04 05:29:48

您需要定义一个事务拦截器。基本上定义一个@Transactional注释并拦截用它注释的所有方法。在拦截器中刚开始、提交或回滚事务。当交易传播出现时,情况会变得更加复杂。所以检查Seam是否没有任何现成的东西http://seamframework.org/Seam3/PersistenceModule

You need do define a transaction interceptor. Basically define a @Transactional annotation and intercept all methods annotated with it. In the interceptor just begin, commit or rollback the transaction. It gets more complicated when transaction propagation comes into the picture. So check if Seam doesn't have anything ready-to-use http://seamframework.org/Seam3/PersistenceModule

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文