【问题标题】:EJB CMT TransactionAttributeType.REQUIRES_NEW doesn't workEJB CMT TransactionAttributeType.REQUIRES_NEW 不起作用
【发布时间】:2010-07-26 12:51:52
【问题描述】:
    @Stateless    @LocalBean
    public class MySLSB {

            @Resource
            SessionContext ctx;
            @PersistenceContext(unitName = "myPU")
            EntityManager em;

            public void T1() {
                em.persist(new MyEntity(1L)); //T1 created!
/* wrong call to plain java object               
 T2();
 */     
//corrected by lookup its business object first 
ctx.getBusinessObject(MySLSB.class).T2();   
     ctx.setRollbackOnly();
            }

            @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
            public void T2() {
                em.persist(new MyEntity(2L)); //T2 created!
            }
        }

客户端调用T1(),首先T2作为一个新的事务应该被提交, 但 T1 会回滚。

预期结果:

T1:插入到myentity set id=1;

T2:插入到myentity set id=2;

T2:提交;

T1:回滚;

-> 在 DB 中创建 id=2 的行。

实际结果:

插入到myentity set id=1;

插入到myentity set id=2;

回滚;

-> 数据库中没有创建任何内容。

有什么问题?非常感谢!

【问题讨论】:

    标签: jakarta-ee transactions ejb-3.0


    【解决方案1】:

    声明自引用@EJB MySLSB me; 并调用me.T2(); 而不是使用ctx.getBusinessObject(MySLSB.class) 可能更容易。但是感觉是一样的。

    【讨论】:

      【解决方案2】:

      @Solution

      问题解决了。我犯了一个天真的错误。

      对 T2() 的调用应该查找其业务对象,对 T2() 的直接调用仅是对其纯 java 对象。

      我更新了上述问题的代码,使一切都按预期工作。

      【讨论】:

        猜你喜欢
        • 2015-09-07
        • 2013-11-24
        • 2012-03-06
        • 1970-01-01
        • 1970-01-01
        • 2011-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多