Spring JTA transaction with JPA and jndi datasource for Websphere

I have several data sources and one database with JPA. I am using websphere 7. I want all these datasouces to be configured as global transactions. I am using below spring configurations, but transactions do not work as expected global transaction. If one db fails, then the other db receives a fee that is not expected as a single global transaction. Can you help me where I'm doing the wrong ones

I have 2 datasouce one as below with id = "us_icfs_datasource" and the other using JPA

<jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/persistenceUnit"/> <bean id="pabpp" class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" /> <!-- Needed for @Transactional annotation --> <tx:annotation-driven/> <jee:jndi-lookup id="US_ICFS_DATASORCE" jndi-name="jdbc/financing_tools_docgen_txtmgr" cache="true" resource-ref="true" proxy-interface="javax.sql.DataSource" /> 

I also added the code below in web.xml

  <persistence-unit-ref> <persistence-unit-ref-name>persistence/persistenceUnit</persistence-unit-ref-name> <persistence-unit-name>persistenceUnit</persistence-unit-name> </persistence-unit-ref> <persistence-context-ref> <persistence-context-ref-name>persistence/persistenceUnit</persistence-context-ref-name> <persistence-unit-name>persistenceUnit</persistence-unit-name> </persistence-context-ref> 

below is my code where i m uses a transaction

 > @Transactional public TemplateMapping addTemplateMapping(User user, > TemplateMapping templateMapping) throws > TemplateMappingServiceException { .... } 
+4
source share
2 answers

In Websphere, you must use this bean to connect to the Websphere transaction manager:

 <bean id="transactionManager" class="org.springframework.transaction.jta.WebSphereUowTransactionManager"/> 

See also article

EDIT:

To use two-phase commit (i.e. to ensure consistency between multiple resources), you will need to use XA data sources. See this article for more details.

+4
source

First of all, your data sources participating in a global transaction should be of type javax.sql.XADataSource.

You also need to set the transaction type in your save module to JTA (not RESOURCE_LOCAL).

And you need to tell your JPA implementation that you want to do global transactions.

0
source

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


All Articles