4

I am having two datasource one for oracle and other for postgresql and both are used in same business method . How i can make this bussiness method transactional using spring @transaction

Business method

@Transactional 
public int getData(){

oracleDao.func1();
postgreDao.func2();
}

In config i have

<bean id="transactionManagerPostGres" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
      </bean>   
  <bean id="transactionManagerOracle" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactoryOracle"/>
 </bean>    

2 Answers 2

5

You're going to need the support of a proper JavaEE container for this, one that supports two-phase-commit and XA transactions. The Oracle and Postgres JDBC drivers both support XA transactions, so that's OK.

The container exposes this to Spring via the JTA API, and Spring uses that using JtaTransactionManager. Your application doesn't have to change, it just treats it like a normal transaction.

How you go about setting this up depends entirely on your JavaEE container, each one has a different way of doing it.

Sign up to request clarification or add additional context in comments.

10 Comments

I am going to deploy the application on servicemix.
@prabha: In that case, you need to dive into the ServiceMix docs, and find out if it has XA JTA support. If not, you need to find another container that does, or find something that integrates with what you have. That's a question specific to that container, and is independent of Spring.
Can I make @transaction annotation to use different transactionManager in the same method?
@prabha: Yes, using <tx:annotation-driven transactionManager="..."/> - see docs at static.springsource.org/spring/docs/3.0.x/…
Note that the JtaTransactionManager replaces both of your existing JpaTransactionManagers.
|
0

Do you mean a two-phase commit? That's a tough problem and depends on the databases and the drivers you use.

1 Comment

i want to set business method as transaction either full commit or rollback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.