如何确定 SqlConnection 是否已登记到 System.Transactions 中? TX 或不?
当我们使用 System.Transactions 中的转换(为实例创建 TransationScope)时,默认情况下所有 Sql 连接 (System.Data.SqlClient.SqlConnection)(但对于 Oracle.DataAccess.OracleConnection 来说并非如此)都会在打开时登记。 这就是所谓的自动入伍。 不错的功能。 但可以通过连接字符串的参数(enlist=false)将其关闭。 在这种情况下,将不会登记正在打开的连接。 但可以稍后手动登记。 所以我的问题是:对于某些给定的 SqlConnection 实例,我如何确定该连接是否已登记(进入 System.Transaction)。 我可以查看参数的连接字符串。 但这是行不通的,因为正如我所说,可以手动登记连接。
When we using a transation from System.Transactions (creating TransationScope for an instance) by default all Sql-connections (System.Data.SqlClient.SqlConnection) (but is't also the true for Oracle.DataAccess.OracleConnection) are enlisted on opening.
That's called auto-enlistment. Nice feature. But it can be turned off throught a connection string's parameter (enlist=false). In that case connection being opened wouldn't be enlisted.
But it can be enlisted manually later.
So my question is: for some given SqlConnection's instance how can I determine whether that connection is enlisted or not (into an System.Transaction).
I can look at connection string for the parameter. But this won't do because as I said connection could be enlisted manually.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该框架似乎不允许这样做。
也许我们可以讨论一下为什么您需要了解这些信息? TransactionScopeOptions 为您提供了关于何时创建事务的一些灵活性。
然而,拒绝“不”的答案,稍后浏览一下源代码,我创建了这段代码,它确实有效。 请注意,通过对框架进行修补,此代码可能随时停止运行!!!!
同样,这是利用 .NET 框架内的私有变量和内部类。 虽然今天有效,但明天可能就不行了。
The framework doesn't appear to allow that.
Perhaps we could discuss why you need to know this information? The TransactionScopeOptions give you some flexibility about when to create transactions.
However, refusing "no" for an answer, a little source browsing later and I've created this code, which DOES work. Note, that this code could cease to function at anytime with patches to the framework!!!!
Again, this is making use of private variables and internal classes within the .NET framework. While it works today, it may not tomorrow.