简体   繁体   English

Spring事务管理使用多个数据源

[英]Spring transaction management to work with multiple data sources

This might be a repetitive question for you, but I couldn't find (atleast I couldn't understand) a satisfactory answer, hence asking again. 这可能是一个重复的问题,但我找不到(至少我无法理解)一个满意的答案,因此再次提问。

I am working with two data sources (MySQL and Oracle). 我正在使用两个数据源(MySQL和Oracle)。 Following is a flow of execution: Main method-A calls method-B (Which writes into Oracle DB) then it(Method-A) calls method-C (Which writes into mySQL DB) then it(Method-A) calls method-D(Which writes into Oracle DB). 以下是执行流程:Main方法-A调用方法-B(写入Oracle DB)然后它(Method-A)调用method-C(写入mySQL DB)然后它(Method-A)调用方法 - D(写入Oracle DB)。

If failure occurs at any of place, everything should be rolled back. 如果在任何地方发生故障,一切都应该回滚。 Currently only changes in Oracle DB are getting rolled back & mySQL DB is not getting rolled back. 目前只有Oracle DB中的更改被回滚,而mySQL DB没有回滚。

I have defined two transactional managers. 我已经定义了两个事务管理器。

=========> First <=========

<tx:annotation-driven transaction-manager="txManager" mode='proxy' proxy-target-class='true’/>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="SessionFactory" />
</bean>
<bean id=“SessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean” parent="AbstractSessionFactory" depends-on="AppConfigHelper”>
<property name="hibernateProperties”> 
...
ORACLE DB Properties
</property>
</bean>
<aop:aspectj-autoproxy/>

==============================
=========> Second <=========

<tx:annotation-driven transaction-manager="txManager2" mode='proxy' proxy-target-class='true'/>
<bean id="txManager2" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="SessionFactory2" />
    <qualifier value="CherryTransaction" />
</bean>
<aop:aspectj-autoproxy/>
<bean id="SessionFactory2" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" parent="AbstractSessionFactory2" depends-on="AppConfigHelper">
    <property name="hibernateProperties">
    ...
    MYSQL DB Properties
    </property>
</bean>

==============================
  • On top of method-A I have used @Transactional annotation 在方法-A之上我使用了@Transactional注释
  • On top of method-B I have used @Transactional annotation 在方法-B之上我使用了@Transactional注释
  • On top of method-C I have used @Transactional("txManager2") annotation 在方法-C之上我使用了@Transactional(“txManager2”)注释
  • On top of method-D I have used @Transactional annotation 在方法-D之上我使用了@Transactional注释

Question Is: 问题是:

  1. Why is MySQL changes are not getting rolled back? 为什么MySQL的变化没有得到回滚?
  2. Is the only way to get this working is to use Global transaction management using JTA? 使这项工作的唯一方法是使用JTA使用全局事务管理吗? (Its a legacy system, and this is the only place where I need to interact with two DBs) (它是一个遗留系统,这是我需要与两个DB交互的唯一地方)
  3. Can you please point me to an example / tutorial where this kind case is handled? 能否请您指出一个处理此类案例的示例/教程?

Sincerely thanks for reading this! 真诚地感谢您阅读本文!

For that to work, AFAIK you'd need to use JTA. 要做到这一点,AFAIK你需要使用JTA。 Even that won't help if you're using a storage engine in MySQL that doesn't support transactions. 如果您在MySQL中使用不支持事务的存储引擎,那么即使这样也无济于事。 With MySQL, only InnoDB and BDB storage engines support transactions . 使用MySQL, 只有InnoDB和BDB存储引擎支持事务

If you are using MySQL with storage engine that supports transactions, you need to configure XA drivers for both Oracle and MySQL datasource and ensure that both datasources are enlisted in the transaction of your container. 如果您将MySQL与支持事务的存储引擎一起使用,则需要为Oracle和MySQL数据源配置XA驱动程序,并确保两个数据源都在容器的事务中登记。 Spring then needs to participate in the same transaction. 然后Spring需要参与同一个事务。 You can't use HibernateTransactionManager for this, but need the JtaTransactionManager, as explained in this thread . 您不能使用HibernateTransactionManager,但需要JtaTransactionManager, 如此线程中所述

So, what you need for this to work is 所以,你需要这个工作

  1. Use InnoDB or BDB storage engines on MySQL 在MySQL上使用InnoDB或BDB存储引擎
  2. Configure XA datasources in your application server instead of regular ones 在应用程序服务器中配置XA数据源而不是常规数据源
  3. Use JtaTransactionManager on Spring configuration 在Spring配置上使用JtaTransactionManager

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM