What is the 'best' way to do distributed transactions across multiple databases using Spring and Hibernate

后端 未结 5 1910
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 04:31

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 bu

5条回答
  •  清酒与你
    2020-11-28 05:30

    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.

提交回复
热议问题