问题描述
我有两个事务资源,数据库和消息队列.因此,我将Atomikos用作XA事务管理器.
I have two transactional resources, database and message queue. So I use Atomikos as the XA transaction manager.
在事务(tx1)中,是否可以并行打开另一个分离的事务(tx2)?
Inside a transaction (tx1), is it possible to open another separated transaction (tx2) in parallel?
在tx2中,它将把一些数据提交到db中,即使tx1可能会失败并最终回滚.
In tx2, it will commit some data into db, even the tx1 might be failed and roll backed eventually.
tx2必须在tx1内完成,好像在tx2中发生错误时也应该回滚tx1.
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?
谢谢.
推荐答案
是的,您可以实现.您谈论的是所谓的嵌套"交易首先,对于Atomikis,必须指定属性 com.atomikos.icatch.serial_jta_transactions = false
Yes, you can achive this. You talk about so named "nested" transactionFirst of all for Atomikis you must specify property com.atomikos.icatch.serial_jta_transactions=false
如果直接使用TransactionManager,则必须在开始tx2(TransactionManager.suspend())之前暂停tx1.提交事务tx2后,您必须恢复tx1.如果执行tx2时发生错误,则必须回滚tx2,恢复tx1并回滚tx1:
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:
示例
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();
}
这篇关于是否可以在JTA(Atomikos)中设置并行事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!