How to Get Transaction Link in Spring Program Transaction

I want to use spring programmatic transaction in a JMS application that receive a message from a queue. At the same time, I want to include a queue in a transactional scope.

Using spring DefaultMessageListnereContainer and introducing a transaction manager into it. However, you do not know how I can get a link to this transaction for software commit or rollback?


I read and understand "message processing in transactions" here http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/jms.html#jms-tx-participation

My 2 requirements are: 1) XA transactions, therefore, introduce JtaTransactionManager, 2) Use program transactions - here I need help how to get a link to a transaction starting with spring in the code so that I can process transactions programmatically

+4
source share
2 answers

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) { //rollback: throw new HoustonException("We've got a problem!"); } 

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.

+9
source

Spring abstracts the main transaction statement with the PlatformTransactionManager .
At run time, we can implement the platform transaction manager shown in the following image.

enter image description here

We can get a link to the Transaction Manager in the spring programming approach in the following two ways:

  • TransactionDefinition interface interface and TransactionStatus interface

Example:

  @Resource private PlatformTransactionManager transactionManager; public void create(String name, Integer age, Integer marks, Integer year){ TransactionDefinition def = new DefaultTransactionDefinition(); TransactionStatus status = transactionManager.getTransaction(def); try { //jmsTemplate statements transactionManager.commit(status); } catch (DataAccessException e) { System.out.println("Error in creating record, rolling back"); transactionManager.rollback(status); throw e; } 
  1. Using a class i.e. TransactionTemplate :

We do not need to explicitly create TransactionDefinition and TransactionStatus objects. TransactionTemplate provides a callback method called execute, where we can add our business logic that we want to associate with the transaction.

There are two types of callback methods that we can use to port our code i.e. TransactionCallbackWithoutResult and TransactionCallback(T)

Example:

 @Resource private TransactionTemplate transactionTemplate; public void deleteUser(final int uid) { transactionTemplate.execute(new TransactionCallbackWithoutResult() { protected void doInTransactionWithoutResult(TransactionStatus paramTransactionStatus) { try{ //statements }catch (Exception e) { //use this to rollback exception in case of exception paramTransactionStatus.setRollbackOnly(); } } }); } public int insertUser(final User user) { //use TransactionCallback handler if some result is returned return transactionTemplate.execute(new TransactionCallback<Integer>() { public Integer doInTransaction(TransactionStatus paramTransactionStatus) { //statements } }); } 
0
source

Source: https://habr.com/ru/post/1393362/


All Articles