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.