JDBC和JMS的Spring事务同步 [英] Spring Transaction Synchronization of JDBC and JMS

查看:173
本文介绍了JDBC和JMS的Spring事务同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在jboss上运行的spring web app,当前配置为使用HibernateTransactionManager进行db事务,JmsTransactionManager用于jms。对于jms,我们使用Camel和ActiveMQ,我们的数据库是DB2。在一个事务中,我需要将一些记录写入数据库并发送两个异步jms消息。 jms消息是事件通知,我只希望在数据库事务提交时发送它们。

I have a spring web app running on jboss that is currently configured to use the HibernateTransactionManager for db transactions and the JmsTransactionManager for jms. For jms we use Camel and ActiveMQ, our database is DB2. Within a transaction I need to write a number of records to the database and send two asynchronous jms messages. The jms messages are event notifications and I only want them to be sent if the database transaction commits.

我愿意接受在jdbc事务已经提交之后与代理通信失败的风险(因此没有发送消息但db已提交)所以我不这样做我认为我需要适当的XA。

I am willing to accept the risk of the communication with the broker failing after the jdbc transaction has already committed (and thus no messages sent but db committed) so I do not think I need proper XA.

我认为我需要的是使用spring transaction同步的尽力而为的事务管理。

I believe that what I need is "best efforts" transaction management using spring transaction synchronization.

Spring文档提示了spring将同步两个事务并在jdbc事务提交后才提交jms事务这一事实 - 但我认为不是很清楚。这里的spring文档 http:// static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#tx-resource-synchronization 没有详细说明它是如何工作的。

The spring documentation sort of hints at the fact that spring will synchronize the two transactions and commit the jms transaction only after the jdbc transaction has been committed - but I don't think it is very clear. The spring documentation here http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#tx-resource-synchronization doesn't go into enough detail about how it works.

我发现其他一些消息来源说spring会做我想做的事,包括下面的一些javadoc,并且我已经编写了一些集成测试,也显示了它。

I have found a couple of other sources that say spring will do what I want including some javadoc below, and I have written some integration tests that also show it.

http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/jms/support/JmsAccessor.html#setSessionTransacted%28boolean%29 此处setSessionTransacted上的javadoc听起来就像我想要的那样。

http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/jms/support/JmsAccessor.html#setSessionTransacted%28boolean%29 The javadoc on setSessionTransacted here sounds like exactly what I want.

从我所看到的,我认为用t创建Camel JmsConfiguration ransacted设置为true就像这样就足够了:

From what I have seen I think creating the Camel JmsConfiguration with transacted set to true like this is enough:

<bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration">
    <property name="connectionFactory" ref="pooledConnectionFactory"/>
    <property name="transacted" value="true"/>
    <property name="concurrentConsumers" value="10"/>
</bean>

然而,我需要说服与我合作的人谁有点怀疑并认为我的集成测试只因为副作用较差而不是有意的弹簧特征而起作用。

However I need to convince someone I work with who is a bit skeptical and thinks that my integration test only works because of a poorly documented side effect rather than a intentional spring feature.

所以我的问题是 - 我是否正确可以依赖Spring来同步事务并始终在jdbc事务之后提交jms事务或者那不是我的事情应该依靠,你可以指出任何明确说明的官方文件吗?我想一般来说这是一个很好的方法,或者我们应该以不同的方式管理这些交易吗?

So my question is - Am I correct that spring can be relied upon to synchronize the transactions and always commit the jms transaction after the jdbc transaction or is that not something that I should rely on, and could you point me at any official documentation that says that clearly? And I guess in general is this a good approach to take or should we be managing these transactions in a different way?

推荐答案

这个文章可能会有所帮助 Spring中的分布式事务,没有XA 。我不认为它具体涵盖了您的案例 - 发送消息+更新数据库。

This article might be of help Distributed transactions in Spring, with and without XA. I don't think it covers your case specifically - sending message + updating database.

这篇关于JDBC和JMS的Spring事务同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆