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

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

问题描述

我有一个在 jboss 上运行的 Spring Web 应用程序,它当前配置为对 db 事务使用 HibernateTransactionManager,对 jms 使用 JmsTransactionManager.对于 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 事务同步进行尽力而为"的事务管理.

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.

从我所见,我认为创建 Camel JmsConfiguration 并将事务设置为 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天全站免登陆