transactionsjdo

JDO Local Transaction Management


In JDO in the situation described below, After methodB() is executed (which has been called from methodA() ), if an exception occurs in methodA() will the rollback take place for the code both in methodA() and methodB() or just methodA() as in methodB() commit has already taken place. Note: PersistenceManager is created on demand and stored in ThreadLocal

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(  );
    }
}

Solution

  • Transactions are not nested, they are independent. The PMs are different so the txns are different. One fails it rolls back, and is nothing to do with the other one