CMT 的 JTA 事务划分到底在哪里受到尊重?

发布于 2024-08-06 14:36:52 字数 1318 浏览 5 评论 0原文

我试图完全理解 JTA 与 CMT 的界限。我遇到的行为是,EJB 上只尊重该方法的第一个 @TransactionAttribute,而具有不同 @TransactionAttribute 注释的同一 bean 的后续方法调用则不受尊重。

示例:

@Stateless
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class Foo implements IFoo {

   @EJB
   private IBar barBean;

   // inherits class transaction annotation of NOT_SUPPORTED
   public void doSomething() {
        barBean.doAction();
   }
}

@Stateless
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class Bar implements IBar {

    public void doAction() {
        Entity entity = bar.find();
        entity.setName("new name");
        // fails with EJBException with TransactionRequiredException as cause
        save(entity);
    }

    public Entity find() {
        // return some persisted entity.
        return em.findById(1);
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public Entity save(entity) {
        em.persist(em.merge(entity));
        em.flush();
    }
}

我看到的行为是 Bar.save() 抛出 TransactionRequiredException。所以这告诉我在 save() 上设置的 REQUIRED 注释不会创建事务。 REQUIRES_NEW 也不起作用。如果我将 save() 移至另一个 EJB,它会按预期工作。

这是否意味着只有第一个 TransactionAttribute 注释受到尊重,无论后续方法调用是否具有不同的注释值?这是一个错误还是预期的行为?我似乎找不到任何具体解释这一点的文档。我很欣赏对此的任何见解。

我的堆栈: EJB 3.0, 顶联要点, GF V2UR2

I'm trying to fully understand the JTA demarcation with CMT. The behavior I am experiencing is that only the first @TransactionAttribute of the method is respected on the EJB and subsequent method invocations of the same bean with different @TransactionAttribute annotations are not.

Example:

@Stateless
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class Foo implements IFoo {

   @EJB
   private IBar barBean;

   // inherits class transaction annotation of NOT_SUPPORTED
   public void doSomething() {
        barBean.doAction();
   }
}

@Stateless
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class Bar implements IBar {

    public void doAction() {
        Entity entity = bar.find();
        entity.setName("new name");
        // fails with EJBException with TransactionRequiredException as cause
        save(entity);
    }

    public Entity find() {
        // return some persisted entity.
        return em.findById(1);
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public Entity save(entity) {
        em.persist(em.merge(entity));
        em.flush();
    }
}

The behavior I'm seeing is that Bar.save() throws a TransactionRequiredException. So this tells me that the REQUIRED annotation set on the save() does not create a transaction. REQUIRES_NEW also does not work. If I move the save() to another EJB, it works as expected.

Does this mean that only the first TransactionAttribute annotation is respected regardless of subsequent method invocations on the same with with different annotation values? Is this a bug or the expected behavior? I cannot seem to find any documentation that concretely explains this. I appreciate any insight on this.

My stack:
EJB 3.0,
Toplink Essentials,
GF V2UR2

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

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

发布评论

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

评论(1

秋心╮凉 2024-08-13 14:36:52

我对 EJB 3 规范的解读是,单个方法上的事务规范会覆盖整个 EJB 的事务规范。因此,您对 REQUIRED 应该适用的期望似乎确实合理,但是......

这仅适用于您将 bean 方法用作 EJB 的情况。当您从一个业务方法 doAction() 直接调用另一个业务方法 save() 时,您没有使用 EJB 引用,因此这只是普通的旧 Java - 不涉及容器,因此容器没有机会干预。

如果您将所需的选项应用于 doAction() 方法,我希望它能起作用。

该理论与您对重构到另一个 EJB 的影响的发现是一致的。

My reading of the EJB 3 spec is that a transaction specification on an individual method overrides that of the EJB as a whole. Therefore your expectations that REQUIRED should apply does seem reasonable, but ...

this is only if you are using the bean method as an EJB. When you make a direct call from one business method doAction() to another, save(), you are not using an EJB reference and hence this is just plain old Java - the container is not involved, so no opportunity for the container to intervene.

If you apply the required option to you doAction() method I would expect this to work.

This theory is consistent with your findings of the effect of refactoring to another EJB.

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