Transactional integration of JMS and DB in Spring

Source: Internet
Author: User

Introduce an article first: http://www.javaworld.com/javaworld/jw-01-2009/jw-01-spring-transactions.html?page=1

But it's a little long, and now I've forgotten what I said. Pay attention to the comments, which refer to the content of the article is wrong.

In spring, if there are both JMS operations and DB operations, there are probably three ways:

1. No use of JTA. JMS not in transaction, DB operation in transaction

A, message processing

b, starting a database transaction

C, Database operations

D, database submission

Success: End

Failed: Back to B Retry

This way the transaction is not integrated, relying purely on our program control, if the final database submission is not successful, you can write down the log, and then manually to correct the data. Example of the database operations in the more important position, in fact, can be reversed, so that the database operation to complete first, have to do JMS operations, see business needs:

A, starting a database transaction

B, Database operations

C, Database submission

d, Message processing

Success: End

Failed: Back to D retry

All we need to do is change the spring configuration

<bean id= "Jmstemplate" class= "Org.springframework.jms.core.JmsTemplate" >
...
<!--This is important ...-->
<property name= "sessiontransacted" value= "false"/>
</bean>
Plus your own program control is good.

2. If you do not use JTA, if you set the property sessiontransacted to True for the above configuration, a second way is created:

A, start a JMS transaction

b, starting a database transaction

C, database and JMS operations

D, submitting database operations

E, submitting JMS operations

The order of the a,b is not certain, but it has no effect on the program, but the order of the d,e is OK. In spring, the database is submitted in spring's Commit method, and JMS is submitted in the Aftercommit method. If the database fails, JMS will of course roll back, but if the database succeeds and JMS fails, there is data inconsistency, plus other measures. And here's a fatal drawback: in some cases, even if JMS fails (like the JMS server down), spring does not throw an exception, and the program thinks everything is fine, but in fact it creates inconsistencies and is hard to find.

3. If you use JTA to set property sessiontransacted to True, JMS and database operations are in the same transaction, there is nothing to say, the safest way, but inefficient.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.