Spring transaction multiple datasource
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()开发者_Python百科;
}
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>
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.
Do you mean a two-phase commit? That's a tough problem and depends on the databases and the drivers you use.
精彩评论