[英]Nested transaction scope .net
我正在使用SQL Server 2008 R2,并尝试使用事务。
首先是有关.net和SQL Server中事务的问题。 如果我有这样的事情
try {
var transactionOption = new TransactionOptions();
transactionOption.IsolationLevel = IsolationLevel.ReadCommitted;
transactionOption.Timeout = TransactionManager.MaximumTimeout;
using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, transactionOption)) {
//create question this creates a new question in the database
Helpers.CreateQuestionBankItem(ref mappedOldNewQuestionItemGuid, missingQuestionBankItems);
//question created
//query database for the code of the newly inserted question, will the database give me the code since Complete has not been called as yet?
scope.Complete();
}
}
catch (Exception ex) {
throw;
}
//query database for the code of the newly inserted question, will the database give me the code since Complete has been called as now?
在这一点上,我应该致电数据库询问新插入的问题的代码。 现在是我的第二个问题,在我问这个链接之前,我已找到嵌套事务 。 根据以上链接,我仍然想问一下我是否有这样的东西
try {
var transactionOption = new TransactionOptions();
transactionOption.IsolationLevel = IsolationLevel.ReadCommitted;
transactionOption.Timeout = TransactionManager.MaximumTimeout;
using (var outerscope = new TransactionScope(TransactionScopeOption.RequiresNew, transactionOption)) {
try {
var transactionOption = new TransactionOptions();
transactionOption.IsolationLevel = IsolationLevel.ReadCommitted;
transactionOption.Timeout = TransactionManager.MaximumTimeout;
using (var innerscope = new TransactionScope(TransactionScopeOption.RequiresNew, transactionOption)) {
//create question this creates a new question in the database
Helpers.CreateQuestionBankItem(ref mappedOldNewQuestionItemGuid, missingQuestionBankItems);
//question created
//query database for the code of the newly inserted question, will the database give me the code since Complete has not been called as yet?
innerscope.Complete();
}
}
catch (Exception ex) {
}
//query database for the code of the newly inserted question, will the database give me the code since Complete has been called as now?
outerscope.Complete();
}
}
catch (Exception ex) {
throw;
}
如果我的innerscope完成,将查询SQL Server给我新创建的问题的代码。
如果内部范围引发异常并且我吞噬了该异常,那么外部范围也会被丢弃怎么办?
调用innerscope.Complete()是否可以完成该内部作用域?
如果要在事务上下文中从故障中恢复,则需要使用事务保存点 。 不幸的是,托管System.Transaction
不支持保存点。 不仅如此,如果您使用事务作用域,则将无法甚至直接使用保存点,因为事务作用域将升级为分布式事务,并且保存点在分布式上下文中不起作用。
您可以使用支持Save()
的保存点的特定于平台的SqlTransaction
。 有关异常处理的异常处理的示例,请参见异常处理和嵌套事务 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.