Java: XA Transaction propagation within many threads

前端 未结 3 2077
离开以前
离开以前 2021-02-06 16:01

How can I use a transaction manager (such as Bitronix, JBoss TS or Atomikos) in a Java SE (not Java EE or Spring) to support the following use case:

Let\'s assume we hav

3条回答
  •  一整个雨季
    2021-02-06 16:22

    Seems like this can be done using Atomikos using SubTxThread

    //first start a tx
    TransactionManager tm = ...
    tm.begin();
    
    Waiter waiter = new Waiter();
    
    //the code that calls the first EIS; defined by you
    SubTxCode code1 = ...
    
    //the associated thread
    SubTxThread thread1 = new SubTxThread ( waiter , code1 );
    
    //the code that calls the second EIS; defined by you
    SubTxCode code2 = ...
    
    //the associated thread
    SubTxThread thread2 = new SubTxThread ( waiter , code2 );
    
    //start each thread
    thread1.start();
    
    thread2.start();
    
    //wait for completion of all calls
    waiter.waitForAll();
    
    //check result
    if ( waiter.getAbortCount() == 0 ) {
        //no failures -> commit tx
        tm.commit();
    } else {
        tm.rollback();
    }
    

提交回复
热议问题