涉及客户端和服务器端数据库和文件系统的分布式事务算法

发布于 2025-01-06 07:46:49 字数 364 浏览 0 评论 0原文

我正在开发一个基于 .Net 的客户端/服务器版本控制系统,该系统基本上涉及

客户端: 嵌入式数据库 文件系统

在服务器端: SQL服务器数据库 文件系统

每次从客户端将新版本上传到服务器时,都会发生以下步骤: 1. 使用新文件元数据(新版本号、时间戳等)更新客户端数据库 2.通过HTTP发送文件到服务器,将文件复制到服务器的文件系统中 3. 文件传输完成后,用新的元数据更新 Sql server db

整个过程被视为一个事务。问题是,一旦出现故障,我们很难根据错误发生的不同阶段来回滚事务。

我的问题是,处理这种类型的分布式提交的算法是什么?我听说过三阶段提交,这适合这种情况吗?

谢谢,

I am working on a .Net based, client/server versioning system which basically involves

At Client side:
an embeded database
file system

At server side:
Sql server db
file system

Everytime a new version is uploaded to the server from the client, the following steps happen:
1. Update the client side db with the new file metadata (new version#, timestamps, etc)
2. Send file to the server via HTTP, copy the file into server's file system
3. When file transfer finishes, update Sql server db with the new meta data

The whole process is treated as a transaction. The problem is, in case of failure, we have a hard time to roll back the transaction based on the different stage the error occurs.

My question is, what is the algorithm to handle this type of distributed commit? I've heard of 3-phase commit, is that a good suit for this scenario?

Thanks,

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

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

发布评论

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

评论(1

甜味拾荒者 2025-01-13 07:46:49

这是您可以尝试的事情,但您必须改进示例..这只是您可以做什么的示例

using (SqlConnection conn1 = new SqlConnection())
{
    using (SqlConnection conn2 = new SqlConnection())
    {
        try
        {
            using (var scope = new System.Transactions.TransactionScope())
            {
                //when opening the connection, its' implicitly enlisted in the transaction scope
                conn1.Open();
                SqlCommand command1 = new SqlCommand(cmdText, conn1);
                //set parameters
                command1.ExecuteNonQuery();

                conn2.Open();
                SqlCommand command2 = new SqlCommand(cmdText, conn1);
                //set parameters
                command2.ExecuteNonQuery();

                //transaction commit/rolls back if an exception is thrown
                scope.Complete();
            }
        }
        catch (System.Transactions.TransactionAbortedException ex)
        {
            //handle aborted transaction
        }
        catch (Exception ex)
        {
            //handle other exceptions
        }
    }
}

this is something that you can try but you will have to improve the example ..this is just an example on what you could do

using (SqlConnection conn1 = new SqlConnection())
{
    using (SqlConnection conn2 = new SqlConnection())
    {
        try
        {
            using (var scope = new System.Transactions.TransactionScope())
            {
                //when opening the connection, its' implicitly enlisted in the transaction scope
                conn1.Open();
                SqlCommand command1 = new SqlCommand(cmdText, conn1);
                //set parameters
                command1.ExecuteNonQuery();

                conn2.Open();
                SqlCommand command2 = new SqlCommand(cmdText, conn1);
                //set parameters
                command2.ExecuteNonQuery();

                //transaction commit/rolls back if an exception is thrown
                scope.Complete();
            }
        }
        catch (System.Transactions.TransactionAbortedException ex)
        {
            //handle aborted transaction
        }
        catch (Exception ex)
        {
            //handle other exceptions
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文