【问题标题】:Is it possible to set up parallel transactions in JTA (Atomikos)是否可以在 JTA (Atomikos) 中设置并行事务
【发布时间】:2011-12-07 15:40:05
【问题描述】:

我有两个事务性资源,数据库和消息队列。所以我使用 Atomikos 作为 XA 事务管理器。

在一个事务(tx1)中,是否可以并行打开另一个单独的事务(tx2)?

在tx2中,它会将一些数据提交到db中,即使tx1最终可能会失败并回滚。

而且 tx2 必须在 tx1 内部完成,好像 tx2 发生错误也应该回滚 tx1。

有谁知道我是怎么做到的?

谢谢。

【问题讨论】:

    标签: database transactions jta xa atomikos


    【解决方案1】:

    是的,您可以做到这一点。您谈论所谓的“嵌套”事务 首先,对于 Atomikis,您必须指定属性 com.atomikos.icatch.serial_jta_transactions=false

    如果您直接使用 TransactionManager 操作,则必须在开始 tx2 之前暂停 tx1 (TransactionManager.suspend())。提交事务 tx2 后,您必须恢复 tx1。如果在执行 tx2 时出错,你必须回滚 tx2,恢复 tx1 并回滚 tx1:

    例子

    TransactionManager tm=...
    
    tm.begin();
    Transaction tx1 = tm.getTransaction();
    //do somethins in tx1;
    tm.suspend(tx1);
    tm.begin();
    Transaction tx2 = tm.getTransaction();
    
    try{
      //do something in tx2
      tm.commit() ;// try to commit tx2
    }cath(Throwable e){
       tx2.rollback();
       tm.resume(tx1)
       tx1.rollback();
       tx1 = null;
    }
    
    if(tx1!=null){
      tm.resume(tx1);
      tm.commit();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 2011-06-10
      • 2013-06-19
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多