Foram Parikh
Foram Parikh

Reputation: 61

Spring JTA transaction with JPA and jndi datasource for Websphere

I am having multiple datasource and one one database configured 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 the transactions are not working as expected global transaction. If one db is failing then the other db is getting commited which is not expected as single global transactions. Can you please help me where i m doing incorrect,

I am having 2 datasouce one as configured below with id="us_icfs_datasource" and another 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" />

also I have added below code 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 using transaction

> @Transactional    public TemplateMapping addTemplateMapping(User user,
> TemplateMapping templateMapping)          throws
> TemplateMappingServiceException {         .... }

Upvotes: 4

Views: 6372

Answers (2)

beny23
beny23

Reputation: 35018

On Websphere you should use this bean to hook into the Websphere transaction manager:

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

See also this article

EDIT:

In order to use 2-phase commit (i.e. ensuring consistency across multiple resources), you will need to use XA data sources. See this article for details.

Upvotes: 4

František Hartman
František Hartman

Reputation: 15086

First of all your data sources that participate in global transaction must be of javax.sql.XADataSource type.

You also have to set transaction type in your persistence unit to JTA (not RESOURCE_LOCAL).

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

Upvotes: 0

Related Questions