Is it possible to set up parallel transactions in JTA (Atomikos)

旧城冷巷雨未停 提交于 2019-12-08 07:02:32

问题


I have two transactional resources, database and message queue. So I use Atomikos as the XA transaction manager.

Inside a transaction (tx1), is it possible to open another separated transaction (tx2) in parallel?

In tx2, it will commit some data into db, even the tx1 might be failed and roll backed eventually.

And tx2 must be done inside tx1, as if error occurred in tx2 should roll back the tx1 also.

Anyone knows how I can achieve this?

Thank you.


回答1:


Yes, you can achive this. You talk about so named "nested" transaction First of all for Atomikis you must specify property com.atomikos.icatch.serial_jta_transactions=false

If you operate with TransactionManager directly you have to suspend tx1 before begining tx2 (TransactionManager.suspend()). After commiting transaction tx2 you have to resume tx1. And if there is an error while execution tx2 you must do rollback tx2, resume tx1 and rollback tx1:

Example

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


来源:https://stackoverflow.com/questions/7766274/is-it-possible-to-set-up-parallel-transactions-in-jta-atomikos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!