Spring - 在JPA事务之后提交JMS事务 [英] Spring - commit JMS Transaction after JPA transaction
问题描述
我有两个交易管理器: JpaTransactionManager
( myJpaTm
)和 JmsTransactionManager
( myJmsTm
)。
I have two transaction managers: JpaTransactionManager
(myJpaTm
) and JmsTransactionManager
(myJmsTm
).
请考虑以下代码:
@Named
public class TestA {
@Inject TestB testB;
@Transactional(transactionManager="myJpaTm")
public void methoda() {
// store stuff in db
testB.methodb();
}
}
@Named
public class TestB {
@Transactional(transactionManager="myJmsTm")
public void methodb() {
// send few JMS messages
}
}
我们有外部JPA事务和内部JMS事务,两者都是分开的,因为我们没有使用分布式事务。
We have outer JPA transaction and inner JMS transaction, both are separated because we are not using distributed transactions.
我想在提交后立即提交JMS事务JPA交易。在这种情况下,当前的JMS事务需要连接到父JPA事务。
I would like to commit JMS transaction right after committing JPA transaction. In this case current JMS transaction would need to hook up to parent JPA transaction.
我不是在寻找分布式事务的替换,我只是想在将数据提交到数据库之后发送JMS消息。
I'am not looking for substitution to distributed transactions, I just would like to send JMS messages after committing data to database.
我知道我可以创建另一个可以调用 methoda
的类,然后再调用 methodb
,但是我想通过连接两个事务来解决它。
I know that I just could create another class that could call methoda
and afterwards methodb
, but I would like to solve it by connection both transactions together.
推荐答案
我以前使用TransactionSynchronizationManager并添加了发送消息块在afterCommit方法中进行同步。基本上你需要在你的实现中放置这样的东西:
I have done this in past using TransactionSynchronizationManager and adding the send message block in afterCommit method for the synchronization. Basically you need to place something like this in your implementation:
@Named
public class TestA{
@Inject
TestB testB;
@Transactional(transactionManager="myJpaTm")
public void methoda() {
// other db stuff
if(TransactionSynchronizationManager.isActualTransactionActive()){
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter(){
@Override
public void afterCommit(){
testB.methodb();
}
});
}
}
}
这篇关于Spring - 在JPA事务之后提交JMS事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!