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.
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