简体   繁体   English

如何在多个数据库上管理事务

[英]How to manage transactions over multiple databases

I have a web application which receives requests to save orders in a database. 我有一个Web应用程序,它接收将订单保存到数据库中的请求。 I want to write to 2 different databases - one Cassandra instance and one PostgreSQL instance. 我想写入2个不同的数据库-一个Cassandra实例和一个PostgreSQL实例。 I am using plain Java and JDBC (with apache DBUtis) with a lightweight web application library at the front. 我在前面使用轻量级Web应用程序库的纯Java和JDBC(以及apache DBUtis)。

What I am unsure about is how to implement transactionality across the two databases, ie if a write to one of the databases fails, then rollback the other write and put an error message in the error log. 我不确定的是如何在两个数据库之间实现事务性,即,如果对其中一个数据库的写操作失败,则回滚另一个写操作,并将错误消息放入错误日志中。

Are there any mechanisms in Java to implement this? Java中是否有任何机制可以实现这一点? I know of such a thing as two phase commit, is that what I would be looking for here? 我知道诸如两阶段提交之类的东西,这就是我在这里要寻找的东西吗? Are there any alternatives? 还有其他选择吗?

Both Cassandra & PostgreSQL support linearizability and compare-and-set (CAS), so you can implement transactions on the client side. Cassandra和PostgreSQL都支持线性化和比较设置(CAS),因此您可以在客户端实现事务。

If you want Serializable Isolation level then you should take a look on the Percolator's transactions . 如果您需要可序列化隔离级别,则应查看过滤器的事务 The Percolator's transactions are quite known in the industry and have been used in the Amazon's DynamoDB transaction library , in the CockroachDB database and in the Google's Pecolator system itself. Percolator的交易在业界已广为人知,并已在Amazon的DynamoDB交易库CockroachDB数据库和Google的Pecolator系统本身中使用。 A step-by-step visualization of the Percolator's transactions may help you to understand it. 渗透过滤器交易的逐步可视化可以帮助您理解它。

If you expect contention and can deal with Read Committed isolation level then RAMP transactions by Peter Bailis may suit you. 如果您期望争用并且可以处理“读已提交”隔离级别,那么Peter Bailis的RAMP事务可能适合您。 I also created a step-by-step RAMP visualization . 我还创建了分步的RAMP可视化

The third approach is to use compensating transactions also known as the saga pattern. 第三种方法是使用补偿交易,也称为“传奇模式”。 It was described in the late 80s in the Sagas paper but became more actual with the raise of distributed systems. Sagas论文在80年代后期描述了这种方法,但是随着分布式系统的兴起 ,这种方法变得更加实际。 Please see the Applying the Saga Pattern talk for inspiration. 请参阅“ 应用传奇模式”演讲以获取灵感。

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

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