Spring Transaction Synchronization of JDBC and JMS

不羁岁月 提交于 2019-12-03 05:55:02

问题


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.

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.

I believe that what I need is "best efforts" transaction management using spring transaction 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.

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 The javadoc on setSessionTransacted here sounds like exactly what I want.

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.

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?


回答1:


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.




回答2:


Official Spring Boot repository contain JTA examples that combine JMS with JDBC based on Atomikos, Bitronix or Java EE server JBoss WildFly.

Additionally I also created few examples that are located in my Github repository. This contains also non-Spring Boot (pure Spring) example.




回答3:


If you are using Local transactions And the usecase is save to database and then send to jms

Then there could be three cases :

  1. Exception just after receiving(before DB and JMS)

No problem everything will be rolledback

  1. After saving to DB , we have exception

If there is an insert operation,there will be mutiple rows in DB due to retries.With each retry , an insert will be done.And for JMS , message will go to DeadLetterQueue

  1. After saving to DB and sending to JMS , we have an exception

    If there is an insert operation,there will be mutiple rows in DB due to retries.With each retry , an insert will be done.And for JMS , message will go to DeadLetterQueue

Now you dont want to use XA, so the solutions could be

1)Check If(message.getJmsRedelivered() {…}

If not , process it

If its redelivered , check if you processed it already

Check if the data is in database based on details in message

Note that re-deliveries are rare so , this check is also rare and there is no overhead

2)If your method is idempotent , then you dont need this check

And regarding XA , XA guarantees that message is delivered only once And synchronize the transaction across multiple resources

But with XA , you have overhead

So if you can manage without XA , it is preferable



来源:https://stackoverflow.com/questions/9229573/spring-transaction-synchronization-of-jdbc-and-jms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!