简体   繁体   English

多个数据库的事务管理使用Spring和Hibernate

[英]Transaction management for multiple database Using Spring & Hibernate

Am coming up with a Spring & Hibernate application which has Inserts/Update to its own system database and it even has to Insert/update other systems with different database types. 我想出了一个Spring和Hibernate应用程序,它有Inserts / Update到它自己的系统数据库,它甚至必须插入/更新具有不同数据库类型的其他系统。

How can i achieve the transaction management with Hibernate while performing with different databases. 如何在使用不同的数据库执行Hibernate时实现事务管理。

-Bharat -Bharat

You can specify two separate transaction managers. 您可以指定两个单独的事务管理器。

    <bean id="transactionManager1"
    class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory1" />
    <qualifier value="account"/>
</bean>

<bean id="transactionManager2"
    class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory2" />
    <qualifier value="businessData"/>
</bean>

Once your set up is done you can use the qualifier to specify the transaction manager: 设置完成后,您可以使用限定符来指定事务管理器:

    public class TransactionalService {

@Transactional("account")
public void setSomethingInAccount() { ... }

@Transactional("businessData")
public void doSomethingInBusinessData() { ... }
}

Assuming you are looking for transactions involving multiple datasources, I highly recommend you give this series of posts by David Syer (spring lead) on your options: 假设您正在寻找涉及多个数据源的交易,我强烈建议您通过David Syer(春季主管)提供以下系列帖子:

http://www.javaworld.com/article/2077963/open-source-tools/distributed-transactions-in-spring--with-and-without-xa.html http://www.javaworld.com/article/2077963/open-source-tools/distributed-transactions-in-spring--with-and-without-xa.html

For example, some databases even support transactions across databases schemas under certain circumstances without distributed transactions (see here ). 例如,某些数据库甚至在某些情况下支持跨数据库模式的事务,而没有分布式事务(参见此处 )。

There are other options other than XA 2-phase commit transaction managers (as they introduce significant overhead and complexity) but they involve different trade-offs. 除XA两阶段提交事务管理器之外还有其他选项(因为它们引入了显着的开销和复杂性),但它们涉及不同的权衡。

This will need to be done by the container where your application runs (eg JBoss App Server) or you'll need to hook into some other transaction manager such as Bitronix. 这需要由运行应用程序的容器(例如JBoss App Server)完成,或者您需要连接到其他事务管理器(如Bitronix)。 Out of the box in a JavaSE application, you'll only get a "best-effort" attempt at managing a transaction between multiple datasources. 在JavaSE应用程序中开箱即用,您只需“尽力”尝试管理多个数据源之间的事务。

You'll need to define connections to both databases using an XA driver and then make sure your transaction manager is leveraging a two-phase commit. 您需要使用XA驱动程序定义与两个数据库的连接,然后确保您的事务管理器正在利用两阶段提交。

If you aren't familiar with Global transactions, start here: http://docs.spring.io/autorepo/docs/spring/4.2.x/spring-framework-reference/html/transaction.html#transaction-global 如果您不熟悉全局事务,请从此处开始: http//docs.spring.io/autorepo/docs/spring/4.2.x/spring-framework-reference/html/transaction.html#transaction-global

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

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