First you need to insert org.springframework.transaction.PlatformTransactionManager - this is a regular bean, like everyone else:
@Resource private PlatformTransactionManager transactionManager;
Now you can use it together with TransactionTemplate :
final TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); transactionTemplate.execute(new TransactionCallback<String>() { @Override public Object doInTransaction(TransactionStatus status) { transactionManager.rollback(status); return ":-("; } });
Pretty much code, here is how you should do it:
@Transactional public void onMessage(Message message) {
If you throw the RuntimeException method from @Transactional , it will automatically roll back from this transaction. Otherwise, it will be done.
Please note that this does not mean that the JMS and the database are working on the same transaction! When you throw an exception, the JMS broker will try to resend the message, however, it is likely that the broker will fail after completing the database transaction. If you need to be 100% sure that both JMS and DB updates are atomic, you need a distributed transaction manager.
source share