交易范围功能
我使用下面的代码来应用事务范围,
TransactionOptions transOption = new TransactionOptions();
transOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transOption))
{
//Code to delete
//Code to insert
scope.Complete();
}
该范围不起作用,如果我在插入中出现错误,记录将被删除而不是回滚
I am using the below code to apply the transaction scope
TransactionOptions transOption = new TransactionOptions();
transOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transOption))
{
//Code to delete
//Code to insert
scope.Complete();
}
the scope is not working where if I have an error in the insert the record is deleted not rolled back
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要告诉我们您正在使用哪个数据库服务器。如果您收到任何异常消息,或者
代码似乎正确,您是否检查了(MSDTC)?检查它是否在您的操作系统上启用。
you need to tell us which database server you are using. and if you are getting any exception messages or not
anyway the code seems correct, did you check the (MSDTC)? check if it's enabled on your OS.
http://msdn.microsoft.com/en-us /library/system.transactions.transactionscope.complete.aspx 状态:
TransactionScope.Complete() 表示范围内的所有操作均已成功完成。
因此,除非您的“要插入的代码”抛出终止使用块的异常,否则您的代码始终调用scope.Complete()并提交数据库更改。
如果您的“要删除的代码”和“要插入的代码”正在内部处理异常,那么它们需要将信息返回到您的使用块以指示一切是否正常。例如,假设您的“代码”块是方法调用,如果一切正常则返回 true,如果失败则返回 false,您可以编写:
http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.complete.aspx states:
TransactionScope.Complete() Indicates that all operations within the scope are completed successfully.
So unless your "Code to Insert" is throwing an exception which terminates the Using block, your code is always calling scope.Complete() and committing your database changes.
If your "Code to Delete" and "Code to Insert" are handling exceptions internally, then they need to return info to your Using block to indicate whether or not everthing worked. For example, assuming your "code" blocks are method calls that return true if all good or false if failed, you can write: