使用Spring和Hibernate跨多个数据库执行分布式事务的'最佳'方法是什么? [英] What is the 'best' way to do distributed transactions across multiple databases using Spring and Hibernate

查看:377
本文介绍了使用Spring和Hibernate跨多个数据库执行分布式事务的'最佳'方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序 - 更像是一个实用程序 - 位于角落并定期更新两个不同的数据库。

这是一个使用Spring应用程序上下文构建的独立应用程序。上下文中配置了两个Hibernate Session Factories,然后使用Spring中配置的Commons DBCP数据源。

目前没有事务管理,但我想补充一些。一个数据库的更新取决于对另一个数据库的成功更新。



该应用程序不在Java EE容器中 - 它由一个静态启动器类一个shell脚本。启动程序类实例化应用程序上下文,然后在其一个bean上调用一个方法。



将数据库更新的事务性设置为最佳的方式是什么?



的'最好',但我认为它应该是'容易设置','易于配置','便宜'和'易于打包和重新分配'的功能。很自然地,FOSS会很好。

解决方案

将事务分配到多个数据库的最佳方式是: p>

有些人会指向XA,但XA(或两阶段提交)是谎言(或市场人士)。

想象一下:在第一阶段告诉XA管理器它可以发送最终提交后,到其中一个数据库的网络连接失败。怎么办?时间到?这会使其他数据库损坏。回滚?两个问题:你不能回滚一个提交,你怎么知道第二个数据库发生了什么?也许在成功提交数据后网络连接失败,只有成功消息丢失了?



最好的方法是将数据复制到一个地方。使用允许您放弃副本并在任何时间继续它的方案(例如,忽略您已拥有的数据或通过ID命令选择并仅请求您的副本的记录> MAX(ID))的方案。用交易来保护这一点。这不是问题,因为您只是从源读取数据,所以当事务因任何原因失败时,您可以忽略源数据库。因此,这是一个普通的单一来源交易。



复制数据后,在本地处理。


I have an application - more like a utility - that sits in a corner and updates two different databases periodically.

It is a little standalone app that has been built with a Spring Application Context. The context has two Hibernate Session Factories configured in it, in turn using Commons DBCP data sources configured in Spring.

Currently there is no transaction management, but I would like to add some. The update to one database depends on a successful update to the other.

The app does not sit in a Java EE container - it is bootstrapped by a static launcher class called from a shell script. The launcher class instantiates the Application Context and then invokes a method on one of its beans.

What is the 'best' way to put transactionality around the database updates?

I will leave the definition of 'best' to you, but I think it should be some function of 'easy to set up', 'easy to configure', 'inexpensive', and 'easy to package and redistribute'. Naturally FOSS would be good.

解决方案

The best way to distribute transactions over more than one database is: Don't.

Some people will point you to XA but XA (or Two Phase Commit) is a lie (or marketese).

Imagine: After the first phase have told the XA manager that it can send the final commit, the network connection to one of the databases fails. Now what? Timeout? That would leave the other database corrupt. Rollback? Two problems: You can't roll back a commit and how do you know what happened to the second database? Maybe the network connection failed after it successfully committed the data and only the "success" message was lost?

The best way is to copy the data in a single place. Use a scheme which allows you to abort the copy and continue it at any time (for example, ignore data which you already have or order the select by ID and request only records > MAX(ID) of your copy). Protect this with a transaction. This is not a problem since you're only reading data from the source, so when the transaction fails for any reason, you can ignore the source database. Therefore, this is a plain old single source transaction.

After you have copied the data, process it locally.

这篇关于使用Spring和Hibernate跨多个数据库执行分布式事务的'最佳'方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆