在C#中如何使用的TransactionScope?如何使用、TransactionScope

2023-09-02 11:49:56 作者:挽妾入墓,

我想使用的TransactionScope,但不断收到下面的异常。该应用程序是一个不同的机器在数据库上运行,如果该事项。我使用SQL Server 2005。

I am trying to use TransactionScope, but keep getting the exception below. The app is running on a different machine than the database, if that matters. I am using Sql Server 2005.

网络访问分布式事务管理器(MSDTC)已被禁用。请启用DTC在安全配置网络访问 使用的组件服务管理工具MSDTC。

"Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using the Component Services Administrative tool."

using (TransactionScope tsTransScope = new TransactionScope())
{
    //Do stuff here
    tsTransScope.Complete();
}

修改

我做了基于反馈的一些chagnes。现在,我得到这个错误:

I made some chagnes based on the feedback. Now I'm getting this error:

{"Error HRESULT E_FAIL has been returned from a call to a COM component."}
{"Communication with the underlying transaction manager has failed."}

解决方案 我想接受的答案解决了最初的问题我得到。第二错误似乎是特定于实体框架。我会后一个问题吧。

Solution I think the accepted answer fixed the initial issue I was getting. The 2nd error seems to be specific to entity framework. I'll post another question for it.

以下是客户端的属性:

Here are the properties on the client:

下面是服务器上的属性:

推荐答案

您需要在此的 Microsoft TechNet文章。这种变化可能必须数据库和应用程序服务器上进行。很多时候,DTC已打开一个数据库服务器上,所以我想看看在应用程序服务器第一。

You need to enable network DTC access as described in this Microsoft TechNet Article. This change may have to be made on both the database and application servers. Often times DTC is already turned on a database server so I'd look at the application server first.

下面是一个屏幕截图中,我们使用除允许远程管理选项是什么:

Here is a screen shot of what we use except for the "Allow Remote Administration" option:

我还没有碰到HRESULT E_FAIL问题,现在你有,但这篇文章 XP SP2和过这种有趣的建议交易:

I have not run into the HRESULT E_Fail issue you are now having but this article on XP SP2 and transactions had this interesting suggestion:

另一种配置设置,你   需要注意的(虽然我认为   它是一种不常见的情况)是   RestrictRemoteClients注册表键。如果   本项的值被设置为2   (RPC_RESTRICT_REMOTE_CLIENT_HIGH),然后   MSDTC网络交易将不   能正常工作。 MSDTC支持   只有RPC_RESTRICT_REMOTE_CLIENT_NONE   (0)和   RPC_RESTRICT_REMOTE_CLIENT_DEFAULT(1)   值。看到   http://www.microsoft.com/technet/prodtechnol/winxppro/maintain/sp2netwk.mspx#XSLTsection128121120120   有关更多信息   RestrictRemoteClients的。

Another configuration setting that you need to be aware (although I consider it to be an uncommon scenario) is RestrictRemoteClients registry key. If the value of this key is set to 2 (RPC_RESTRICT_REMOTE_CLIENT_HIGH) then MSDTC network transactions will not be able to work properly. MSDTC supports only RPC_RESTRICT_REMOTE_CLIENT_NONE (0) and RPC_RESTRICT_REMOTE_CLIENT_DEFAULT (1) values. See http://www.microsoft.com/technet/prodtechnol/winxppro/maintain/sp2netwk.mspx#XSLTsection128121120120 for more info on RestrictRemoteClients.

最后,虽然不是专门针对您的问题需要注意使用的TransactionScope 类很重要的一点是,它的默认设置是利用的序列化的事务隔离级别。序列化是最严格的隔离级别,并坦言其令人惊讶的是它被选为默认。如果你不需要锁定我会极力推荐的隔离级别设置为限制较少的选项(READCOMMITTED)这一级别的实例化时,一个的TransactionScope

Finally, while not specific to your issue a very important thing to note about using the TransactionScope class is that its default setting is to utilize a Transaction Isolation Level of Serializable. Serializable is the most restrictive of the isolation levels and frankly its surprising that it was chosen as the default. If you do not need this level of locking I would highly recommend setting the isolation level to a less restrictive option (ReadCommitted) when instantiating a TransactionScope:

var scopeOptions = new TransactionOptions();
scopeOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
scopeOptions.Timeout = TimeSpan.MaxValue;

using (var scope = new TransactionScope(TransactionScopeOption.Required,
    scopeOptions))
{
    // your code here
}
 
精彩推荐
图片推荐