1

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

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

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

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

任何人都知道我怎么能做到这一点?

谢谢你。

4

1 回答 1

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();
}
于 2012-01-13T14:30:12.200 回答