- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
SqlConnection 在事务中“登记”是什么意思?这是否只是意味着我在连接上执行的命令将参与事务?
如果是这样,在什么情况下 SqlConnection 会自动加入环境 TransactionScope 事务?
请参阅代码注释中的问题。我对每个问题的答案的猜测都在括号中的每个问题之后。
场景 1:在事务范围内打开连接
using (TransactionScope scope = new TransactionScope())
using (SqlConnection conn = ConnectToDB())
{
// Q1: Is connection automatically enlisted in transaction? (Yes?)
//
// Q2: If I open (and run commands on) a second connection now,
// with an identical connection string,
// what, if any, is the relationship of this second connection to the first?
//
// Q3: Will this second connection's automatic enlistment
// in the current transaction scope cause the transaction to be
// escalated to a distributed transaction? (Yes?)
}
//Assume no ambient transaction active now
SqlConnection new_or_existing_connection = ConnectToDB(); //or passed in as method parameter
using (TransactionScope scope = new TransactionScope())
{
// Connection was opened before transaction scope was created
// Q4: If I start executing commands on the connection now,
// will it automatically become enlisted in the current transaction scope? (No?)
//
// Q5: If not enlisted, will commands I execute on the connection now
// participate in the ambient transaction? (No?)
//
// Q6: If commands on this connection are
// not participating in the current transaction, will they be committed
// even if rollback the current transaction scope? (Yes?)
//
// If my thoughts are correct, all of the above is disturbing,
// because it would look like I'm executing commands
// in a transaction scope, when in fact I'm not at all,
// until I do the following...
//
// Now enlisting existing connection in current transaction
conn.EnlistTransaction( Transaction.Current );
//
// Q7: Does the above method explicitly enlist the pre-existing connection
// in the current ambient transaction, so that commands I
// execute on the connection now participate in the
// ambient transaction? (Yes?)
//
// Q8: If the existing connection was already enlisted in a transaction
// when I called the above method, what would happen? Might an error be thrown? (Probably?)
//
// Q9: If the existing connection was already enlisted in a transaction
// and I did NOT call the above method to enlist it, would any commands
// I execute on it participate in it's existing transaction rather than
// the current transaction scope. (Yes?)
}
最佳答案
自从提出这个问题以来,我已经做了一些测试,并且我自己找到了大部分(如果不是全部)答案,因为没有其他人回答。如果我错过了什么,请告诉我。
Q1: Is connection automatically enlisted in transaction?
enlist=false
在连接字符串中指定。连接池找到一个可用的连接。可用连接是未登记在事务中或登记在同一事务中的连接。
Q2: If I open (and run commands on) a second connection now, with an identical connection string, what, if any, is the relationship of this second connection to the first?
Q3: Will this second connection's automatic enlistment in the current transaction scope cause the transaction to be escalated to a distributed transaction?
Transaction.Current.TransactionInformation.DistributedIdentifier
检查非空 GUID 来确认。 .
Q4: If I start executing commands on the connection now, will it automatically become enlisted in the current transaction scope?
Q5: If not enlisted, will commands I execute on the connection now participate in the ambient transaction?
Q6: If commands on this connection are not participating in the current transaction, will they be committed even if rollback the current transaction scope?
Q7: Does the above method explicitly enlist the pre-existing connection in the current ambient transaction, so that commands I execute on the connection now participate in the ambient transaction?
EnlistTransaction(Transaction.Current)
在当前事务范围内显式登记现有连接。 .您还可以通过使用 DependentTransaction 在事务中的单独线程上登记一个连接,但像以前一样,我不确定同一个事务中针对同一个数据库的两个连接如何交互......并且可能会发生错误,并且当然,第二个登记的连接会导致事务升级为分布式事务。
Q8: If the existing connection was already enlisted in a transaction when I called the above method, what would happen? Might an error be thrown?
TransactionScopeOption.Required
被使用,并且连接已经登记在事务范围事务中,则没有错误;实际上,没有为作用域创建新事务,事务计数 (
@@trancount
) 没有增加。但是,如果您使用
TransactionScopeOption.RequiresNew
,然后在尝试在新事务范围事务中登记连接时收到有用的错误消息:“连接当前已登记事务。完成当前事务并重试。”是的,如果您完成了连接被登记的事务,您可以安全地在新事务中登记该连接。
BeginTransaction
在连接上,当您尝试在新的事务范围事务中登记时会抛出一个略有不同的错误:“无法登记在事务中,因为本地事务正在进行连接。完成本地事务并重试。”另一方面,您可以安全地拨打
BeginTransaction
在
SqlConnection
而它在事务范围事务中登记,这实际上会增加
@@trancount
一,不像使用嵌套事务范围的Required选项,这不会导致它增加。有趣的是,如果您继续使用
Required
创建另一个嵌套事务范围。选项,你不会得到错误,因为已经有一个事件的事务范围事务没有任何变化(记住当事务范围事务已经事件并且使用
@@trancount
选项时
Required
不会增加)。
Q9: If the existing connection was already enlisted in a transaction and I did NOT call the above method to enlist it, would any commands I execute on it participate in its existing transaction rather than the current transaction scope?
关于c# - 在什么情况下 SqlConnection 会自动加入环境 TransactionScope 事务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2884863/
我有一个应用程序,我想将多个数据库保存到一个事务中。如果他们中的任何一个失败了,我想把整个事情都推回去。但是,我想在回滚事务之前知道其中哪些失败(或成功)。 我有一个带有内部循环的外部 Transac
我刚刚开始玩 Dapper。到目前为止我很喜欢它。 dapper 不能与 TransactionScope 一起使用吗?我注意到,即使我从不调用 TransactionScope.Complete,我
您好首先感谢您对这个问题的关注;有没有办法在c#中实现这样的事务 using (Transactionscope x=new Transactionscope ()) { Thead A()=>
这是使用事务范围的正确方法吗: 我有一个代表事物的一部分的对象: public class ThingPart { private DbProviderFactory connectionFa
我正在尝试找到在使用 NHibernate 的 Web 应用程序中处理事务的最佳解决方案。 我们使用 IHttpModule 并在 HttpApplication.BeginRequest 我们打开一
我面临以下问题: 在我的项目中,我在应用程序正在使用的同一个数据库中进行了错误登录。这意味着,如果发生错误,那么在每个 catch 中,都会将错误存储到 DB 中。 然而,问题是在使用事务时。发生错误
我想知道在处理多线程时如何正确使用 TransactionScope 类? 我们在主线程中创建一个新的作用域,然后生成几个工作线程,我们希望它们参与主作用域,因此,如果作用域从未完成,则在每个工作线程
任何人都可以给我一个关于使用 TransactionScope 和 NHibernate 的快速概述吗?我需要对 session /IEnlistmentNotification/等做一些特别的事情吗
如果我执行以下操作: Using scope = New TransactionScope() entries.Content.ReadAs(Of IList(Of WebMaint
当 TransactionScope 首次推出时,我在让它在我的开发机器 (XP) 和我们的数据库服务器 (Windows Server 2003) 之间工作时遇到了一些严重的问题。 当我进一步研究时
我在基于 Silverlight 和 RIA 服务的项目中使用 TransactionScope 类。每次我需要保存一些数据时,我都会创建一个 TransactionScope 对象,使用 Oracl
TransactionScope 是否适用于封闭的数据库连接? using (var transaction = new TransactionScope(TransactionScopeOption
我有两个 PL/SQL 存储过程,每个过程都处理自己的事务(开始/提交和发生错误时的回滚)。从 .Net 代码我调用这两个 SP,如下所示。 using (TransactionScope ts
我在我的应用程序中使用了 TransactionScope 类,因为我需要以交互方式、自然地执行一些操作(要么全部成功,要么全都不成功)。 由于此操作不仅与数据库相关,我通过实现 IEnlistmen
您好,有没有办法检查使用 TransactionScope 的事务是提交还是回滚? 最佳答案 在处理 TransactionScope 之前,实际的提交不会发生。如果 Commit 由于任何原因失败,
我有一个 Web 应用程序,许多用户在任何给定时刻都在使用它。从数据库的角度来看,他们在系统中移动时不断地从任意数量的表中写入和读取数据。此外,我们还有许多将数据导入该系统的流程。 今天,在调试导入过
我正在开发一个使用 C# 和 SQL Server 作为数据库的 .NET Web 应用程序,我是这两种技术的新手。当我尝试同时保存大量信息时遇到问题。 我的代码很简单: SqlConnectio
您好,我在我的应用程序的多个位置使用了 TransactionScope。喜欢: using (var scope = new TransactionScope()) { ToDo1(
我在服务器端代码 (WCF) 中使用 TransactionScope 类时遇到性能问题。 我的代码从客户端获取请求,创建 TransactionScope 并执行短操作(通常最多 100 毫秒)。
我正在处理一个几乎不存在事务的遗留项目,这会在出现错误时导致大量的部分提交。 SQL 调用混合了原始 ADO、Linq to Entities 和 EntityFramework,我想让它们在事务中运
我是一名优秀的程序员,十分优秀!