简体   繁体   English

.NET+SQL Server 2005 - 如何回滚事务但保留日志

[英].NET+SQL Server 2005 - How to rollback transaction but keep the log

We have a system that has both a .NET and SQLServer component.我们有一个同时具有 .NET 和 SQLServer 组件的系统。 The .NET component is responsible to start the process and enclose the calls to stored procedures in a transaction. .NET 组件负责启动进程并将对存储过程的调用包含在事务中。

That process is done using mostly stored procedures on the database, which all write to our own log table.该过程主要使用数据库上的存储过程完成,这些过程都写入我们自己的日志表。

Now, the problem is that the transaction, being started in the .NET code, will be enclosing all others that are used in the database code, and if one of the child transactions rolls back the log table is gone.现在,问题是在 .NET 代码中启动的事务将包含在数据库代码中使用的所有其他事务,如果其中一个子事务回滚,则日志表将消失。

Is there any way to keep the logs, rolling back everything else?有没有办法保留日志,回滚其他所有内容?

Thanks in advance.提前致谢。

Use a table variable to store the error: this is unaffected by transactions使用表变量来存储错误:这不受事务的影响

Or use SET XACT_ABORT ON in the procs which implicitly rolls back and log after error或者在 procs 中使用 SET XACT_ABORT ON 隐式回滚并在错误后记录

Is Transaction SavePoints can help you? Transaction SavePoints 可以帮助您吗?

This is exactly data base level solution :这正是数据库级别的解决方案

Savepoints offer a mechanism to roll back portions of transactions.保存点提供了一种回滚部分事务的机制。 You create a savepoint using the SAVE TRANSACTION savepoint_name statement.您可以使用 SAVE TRANSACTION savepoint_name 语句创建一个保存点。 Later, you execute a ROLLBACK TRANSACTION savepoint_name statement to roll back to the savepoint instead of rolling back to the start of the transaction.稍后,您执行 ROLLBACK TRANSACTION savepoint_name 语句以回滚到保存点,而不是回滚到事务的开始。

Also you can reffer to SqlTransaction.Save Method for a C# solution , hope this is exatcly what you are looking for:您也可以参考SqlTransaction.Save Method for a C# 解决方案,希望这正是您正在寻找的:

Creates a savepoint in the transaction that can be used to roll back a part of the transaction, and specifies the savepoint name.在事务中创建可用于回滚事务的一部分的保存点,并指定保存点名称。

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

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