0

在下面描述的情况下,在 JDO 中,AftermethodB()被执行(已从 调用methodA()),如果发生异常,是否会在提交中和或就像在提交中methodA()发生的代码一样进行回滚。注意:PersistenceManager 按需创建并存储在 ThreadLocal 中methodA()methodB()methodA()methodB()

methodA() {
    PersistenceManager mgr = getPersistenceManager(  );
    Transaction trans;

    trans = mgr.currentTransaction(  );
    try {
        trans.begin(  );
        methodB();

        //some delete/update code
        // An exception occurs

        trans.commit(  );        
    }
    catch( Exception e ) {
        e.printStackTrace(  );
    }
    finally {
        if( trans.isActive(  ) ) {
            trans.rollback(  );
        }
        mgr.close(  );
    }
}

methodB() {
    PersistenceManager mgr = getPersistenceManager(  );
    Transaction trans;

    trans = mgr.currentTransaction(  );
    try {
        trans.begin(  );
        //code
        trans.commit(  );        
    }
    catch( Exception e ) {
        e.printStackTrace(  );
    }
    finally {
        if( trans.isActive(  ) ) {
            trans.rollback(  );
        }
        mgr.close(  );
    }
}
4

2 回答 2

0

事务不是嵌套的,它们是独立的。PM不同,因此txns不同。一个失败它会回滚,并且与另一个无关

于 2013-05-10T17:10:03.467 回答
0

methodA 和 methodB 是不同事务的第二部分,即非原子事务原因:-

1> using same PM instance for two different transaction also doesn't ensure two different transaction are using same transaction instance id always due to there is no guarantee that nested method B object to be persisted is in 'persistance-clean' state always (side effects of persisting object in dirty state) and Method B transaction is in not getting propagated to method A as before , it's getting commited before then returing to A again.

2> transaction is not contextual and hence transaction will never be commited for the whole session or method sequence.

3> As referenced from spring template , for a particular transaction only once the transaction instance are always retrieved and whole the whole transaction is completed then only it's commited ,([it's also indirectly mentioned in jdo document indirectly : [1]: http://www.datanucleus.org/products/accessplatform_4_1/jdo/transactions.html#spring). One possible way would be, method A should act as a subject or observer for transaction here and method B should be a subscriber and when all the subscriber are notified against a particular transaction , then the transaction should be committed.

For more please refer : Patterns for propagating changes to nested objects

于 2018-03-01T22:31:21.373 回答