如何在单个事务下执行多个操作 [英] How to perform multiple operations in under single transaction

查看:20
本文介绍了如何在单个事务下执行多个操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,需要将记录添加到表中,然后 - 如果添加了记录,则在云上创建资源,如果在云上创建资源,则使用资源标识符更新表中的记录.因此,它们是 3 个操作,当其中任何一个操作失败时,我想恢复所有操作.

I have a scenario where it requires to add a record in to table, then - creating a resource on the cloud if record is added, then update the record in table with the resource identifier if resource is created on cloud. So, they are 3 operations and I want to revert all of it when any of them doesn't succeed.

我们一次性拥有多个数据库操作的 TransactionScope,但我想知道如何实现这一点?感谢您的帮助!

We have TransactionScope for Multiple Db Operations in one go but I'm wondering how to achieve this? Appreciate your help!

编辑

PS:可能有任意数量的操作 - 比如说一个序列中的 10 个或更多,它们甚至可能与数据库操作无关.他们可能只是按顺序创建 10 个文件 - 因此,当任何文件创建失败时 - 应删除/撤消所有以前的文件.

PS: There could be any number of operations like that - say 10 or more in a sequence, and they may not even related to DB operations. They could just be creating 10 files in a sequence - so when any of the file creation fails - all the previous files should be deleted/undone.

推荐答案

走一个命令模式怎么样?它可能不是完美的命令模式实现,但非常接近.见下文:

How about going a command pattern way? It's may not be perfect command pattern implementation but something very close. See below:

public interface ICommand {
    ICommandResult Execute();
    ICommandResult Rollback();
}

public interface ICommandResult {
    bool Success { get; set; }
    object Data { get; set; }
    Exception Error { get; set; }
}

public class CommandResult : ICommandResult { 
    public bool Success { get; set; }
    public object Data { get; set; }
    public Exception Error { get; set; }
}

public class AddToDBCommand : ICommand {
    private ICommandResult result;
    private int newRecordId;

    public AddToDBCommand(<params_if_any>) {
        result = new CommandResult();
    }

    public ICommandResult Execute() {
        try {
            // insert record into db
            result.Success = true;
            result.Data = 10; // new record id
        }
        catch (Exception ex) {
            result.Success = false;
            result.Error = ex;
        }
        return result;
    }

    public ICommandResult Rollback() {
        try {
            // delete record inserted by this command instance
            // use ICommandResult.Data to get the 'record id' for deletion
            Console.WriteLine("Rolling back insertion of record id: " + result.Data);
            // set Success
        }
        catch(Exception ex) {
            // set Success and Error
            // I'm not sure what you want to do in such case
        }
        return result;
    }
}

同样,您将创建用于创建云资源和更新数据库中的记录的命令.在主代码中,您可以保存 ICommand 对象的集合并执行每个对象.

Similarly you would create commands for creating cloud resource and updating record in db. In main code you can hold collection of ICommand objects and execute each one.

var commands = new List<ICommand>
{
    new AddToDBCommand(<params_if_any>),
    new AddToCloudCommand(<params_if_any>),
    new UpdateInDBCommand(<param_if_any>)
};

然后在循环中你可以调用Execute,如果它返回Success = false然后在集合中记录当前命令索引并在调用Rollback<的同时向后循环/code> 在每个命令上.

Then in the loop you can call Execute, if it returns Success = false then record the current command index in collection and loop backward whilst calling Rollback on each command.

这篇关于如何在单个事务下执行多个操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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