如何编写“分布式事务”?

发布于 2024-08-14 16:57:44 字数 282 浏览 12 评论 0原文

过去我所有的数据库需求都是通过 sqlite 解决的。现在设计一个可能很大的数据库,我研究了“分布式事务”。我该如何编程?我发现一些结果解释了什么是分布式事务,但没有解释如何对其进行编程。

我知道在代码中我有一些交易,我可能想将它们批量化为更大的交易。我使用.NET。我该如何做一些事情,比如创建一个用户,他的 PK 位于一个数据库中,而他的用户信息(例如名称和设置)位于另一个数据库中。这可能是一个单独的问题,但如果我有两个功能。一个更新媒体描述,另一个更新可能位于同一服务器或单独服务器上的内容。我如何嵌套这些事务并仅在最后提交?

In the past all my db needs were solved by sqlite. Now designing a db that can potentially be large i looked into "Distributed Transaction". How do i program that? I found some results explaining what distributed transaction is but non explained how to program it.

I know in code i have a few transactions that i may want to batch into a larger transaction. I use .NET. How might i do something like create a user where his PK is in one database and his user information such as name and settings are in another database. This may be a separate question but if i have two functions. One updates a media description and the other updates the content which could be on the same server or separate. How i nest these transactions and only commit at the end?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好倦 2024-08-21 16:57:44

有关该概念的介绍,请参阅维基百科

要在 .NET 中使用,请查看 System.Transactions< /code>命名空间,尤其是 TransactionScope 类。这将使您能够使用 DTC。如果您查看 System.Data.SQLite 项目,那么您可以看到如何他们已将 DTC 与 SQLite 集成。

For an introduction to the concept, see Wikipedia.

For use in .NET, look at the System.Transactions namespace, especially the TransactionScope class. This will enable you to hook into the use of DTC. If you look at the System.Data.SQLite project then you can see how they have integrated DTC with SQLite.

过气美图社 2024-08-21 16:57:44

您描述的方法可能会导致性能问题。每个请求都需要一个涉及多个数据库的事务。那是昂贵的。例如,如果您需要在同一事务中涉及 Web 服务调用和数据库请求,则分布式事务可能是一个很好的解决方案。

如果您需要可扩展性来提高未来重负载下的性能,您可以考虑集群。您将运行简单(非分布式)事务,而不是在不同服务器之间分配单个用例的各个部分。而且您将受益于可扩展性,因为不同的请求将由不同的服务器处理。

关于聚类的主要目的及其在各种情况下的适用性有不同的看法。我认为这取决于领域,需要仔细分析。有关聚类的一些链接:
数据库集群 作者:Neil McAllister,
使用 MSCS 的 SQL Server 2000 数据库集群概述(已过时),
聚类(计算) 来自 Wikipedia,以及 聚类算法 - 用于一般知识。

我建议您看一下 Martin Fowler 的 Errant Architectures (主要讨论分布式计算) ,但也适用于事务),分布式事务概述 来自 MSDN,以及以下两种观点:

分发真的那么糟糕吗?和
(关于)马丁·福勒的第一分配定律

Approach that you described may lead to performance problems. Each request needs a transaction involving several databases. That is costly. Distributed transactions may be a good solution if you need, for example, involve a web service call and a DB request in the same transaction.

If you will need scalability to improve performance under heavy load in future, you might consider clustering. Instead of distributing parts of a single use-case among different servers, you will run simple (not distributed) transactions. And you will have benefit of scalability, because different requests will be handled by different servers.

There are different opinions about the main purpose of clustering and its applicability in various situations. I think it depends on domain and needs careful analysis. Some links regarding clustering:
Database Clustering by Neil McAllister,
Overview of SQL Server 2000 Database Clustering using MSCS (outdated),
Clustering (computing) from Wikipedia, and Clustering Algorithms - for general knowledge.

I would recommend you to take a look at Errant Architectures by Martin Fowler (primarily talking about distributed computing, but also applicable to transactions), Distributed Transactions Overview from MSDN, and these two opinions:

Is Distribution really that bad? and
(About) Martin Fowler's First Law of Distribution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文