【问题标题】:Data are not visible at support transation block数据在支持交易块不可见
【发布时间】:2014-01-06 10:30:22
【问题描述】:

我的方法调用如下。

@TransactionAttribute(TransactionAttributeType.SUPPORTS
void M1() {
   M2();
   M3();
}

@TransactionAttribute(TransactionAttributeType.REQUIRED)    
void M2(){
   //saving x on data base
}

@TransactionAttribute(TransactionAttributeType.SUPPORTS)
void M3(){
    //accessing x from data base
}

问题是,有时值 x 在方法 M3 中不可用。

任何机构都可以说这里可能出现的问题是什么?

【问题讨论】:

    标签: java jakarta-ee ejb jta


    【解决方案1】:

    你的例子有两种情况,结果取决于事务是否已经在M1开始,让我告诉你它是如何工作的

    public void M1() {
       //transaction doesn't exist now
       M2();
       //transaction context has been created and ended due to REQUIRED attribute
       M3(); 
       //now you see result in DB because it was commited in M2s transaction 
    }
    
    public void M1() {
       //transaction already exists from method which called M1
       M2(); 
       //joins the transactional context
       M3();  //gets attached to the same context
       // and now whole transaction gets commited and only now you can be sure that you can read from DB safely
    }
    

    希望对您有所帮助,解决方案也将标记M1 REQUIRED。

    【讨论】:

    • 非常感谢,我会用我的总代码检查第二种情况:)
    • 还有一个问题。我可以通过向 M2 添加返回类型并根据 M2 的返回值调用 M3 来解决此问题
    • @Petr Mensik。当 M3 加入 M2 事务时,它不应该有权访问同一事务中未提交的更改吗?我想这取决于隔离级别,也可能取决于底层数据库行为。
    • @Gab 也是这样,也许 OP 可以告诉我们他的隔离级别是什么
    • @amith 老实说,我不确定(我已经有一段时间没有使用 EJB 了),试试这个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 2013-10-28
    • 2018-04-11
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 2010-12-20
    相关资源
    最近更新 更多