简体   繁体   English

没有事务的回滚SQL

[英]Rollback SQL without transaction

I have a windows service that uploads data to a database and a MVC-app that utilises said service. 我有一个Windows服务,该服务将数据上传到数据库,还有一个使用上述服务的MVC应用。 The way it works today is something like this: 今天的工作方式是这样的:

Upload(someStuff);
WriteLog("Uploaded someStuff");
ReadData(someTable);
WriteLog("Reading someTable-data");
Drop(oldValues);
WriteLog("Dropping old values");


private void Upload(var someStuff)
{
    using(var conn = new connection(connectionstring))
    {
        //performQuery
    }
}

private void WriteLog(string message)
{
    using(var conn = etc..)
        //Insert into log-table
}

private string ReadData(var table)
{
    using etc..
        //Query
}
///You get the gist.

The client can then see the current status of the upload through a query to the log-table. 然后,客户端可以通过查询日志表来查看上载的当前状态。

I want to be able to perform a rollback if something fails. 如果某件事失败,我希望能够执行回滚。 My first thought was to use a BeginTransaction() and then lastly a transaction.Commit() , but that would make my status-message behave bad. 我首先想到的是使用BeginTransaction() ,最后使用transaction.Commit() ,但这会使我的状态消息表现不佳。 It would just go from "starting upload" and then fastforward to the last step where it would wait for a long time before "Done". 它只是从“开始上传”开始,然后快进到最后一步,在“完成”之前它将等待很长时间。

I want the user to be able to see if the process is stuck on some specific step, but I still want to be able to perform a full rollback if something unexpected happens. 我希望用户能够看到该过程是否停留在某个特定步骤上,但是如果发生意外情况,我仍然希望能够执行完整的回滚。

How do I achieve this? 我该如何实现?

Edit: I don't seem to have been clear in my question. 编辑:我似乎不太清楚我的问题。 If I do a separate connection for the logging, that would indeed work-ish. 如果我为日志记录做一个单独的连接,那确实可行。 The problem is that the actual code will execute super-fast so the statusmessages would pass so fast that the user wouldn't even be able to see them before the final "committing"-message that would take 99% of the upload-time. 问题在于实际代码将以超快的速度执行,因此状态消息传递得如此之快,以至于用户甚至在最终的“提交”消息之前将无法看到它们,这将占用上载时间的99%。

Design your table so that it has a (P)ending, (A)ctive (D)eleted flag - then to perform an update, new records are created called 'pending' Status P - your very final stage is to change the current Active to Deleted, and the Pending to Active (you could do that in a transaction). 设计表,使其具有(P)结束,(A)主动(D)删除标志-然后执行更新,创建新记录,称为“待处理”状态P-最后一步是更改当前活动目录到已删除,待处理为有效(您可以在事务中执行此操作)。 At your leisure, you can then delete the Status D (deleted) records at some time. 然后,您可以随时删除状态D(已删除)记录。

In the event of an error, the 'pending' record could become Deleted 如果发生错误,“待定”记录可能会被删除

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

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