2008-11-26 45 views
5

我想为我的Sql 2000数据库的Linq-to-Sql操作设置一个简单的事务。使用的TransactionScope它看起来像这样:针对Sql Server 2000的TransactionScope错误 - 伙伴事务管理器已经禁用了对远程/网络事务的支持

using (TransactionScope transaction = new TransactionScope()) 
{ 
    try 
     { 
     Store.DBDataContext dc = new Store.DBDataContext(); 
     Store.Product product = GetProduct("foo"); 
     dc.InsertOnSubmit(product); 
     dc.SubmitChanges(); 
     transaction.Complete(); 
    } 
    catch (Exception ex) 
    {     
     throw ex; 
    } 
} 

不过,我不断收到以下错误:

合作伙伴事务管理器已禁用其远程/网络事务的支持。 (来自HRESULT的例外:0x8004D025)

但是,如果我使用传统事务设置事务,它工作正常。所以这工作得很好:

Store.DBDataContext dc = new Store.DBDataContext(); 
try 
{ 
    dc.Connection.Open(); 
    dc.Transaction = dc.Connection.BeginTransaction(); 
    Store.Product product = GetProduct("foo"); 
    dc.InsertOnSubmit(product); 
    dc.SubmitChanges(); 
    dc.Transaction.Commit(); 
} 
catch (Exception ex) 
{ 
    dc.Transaction.Rollback(); 
    throw ex; 
} 
finally 
{ 
    dc.Connection.Close();  
    dc.Transaction = null; 
} 

我想知道如果TransactionScope是做不同的事情下盖比我的第二个实施。如果没有,我不使用TransactionScope会失去什么?此外,关于导致错误的任何指导也会很好。我已经确认MSDTC在sql server和我的客户端机器上运行。

回答

6

到这里看看:

快速交易与System.Transactions中和Microsoft SQL Server 2000 http://blogs.msdn.com/florinlazar/archive/2005/09/29/475546.aspx

在这里:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=230390&SiteID=1

First verify the "Distribute Transaction Coordinator" Service is running on both database server computer and client computers
1. Go to "Administrative Tools > Services"
2. Turn on the "Distribute Transaction Coordinator" Service if it is not running

If it is running and client application is not on the same computer as the database server, on the computer running database server
1. Go to "Administrative Tools > Component Services"
2. On the left navigation tree, go to "Component Services > Computers > My Computer" (you may need to double click and wait as some nodes need time to expand)
3. Right click on "My Computer", select "Properties"
4. Select "MSDTC" tab
5. Click "Security Configuration"
6. Make sure you check "Network DTC Access", "Allow Remote Client", "Allow Inbound/Outbound", "Enable TIP" (Some option may not be necessary, have a try to get your configuration)
7. The service will restart
8. BUT YOU MAY NEED TO REBOOT YOUR SERVER IF IT STILL DOESN'T WORK (This is the thing drove me crazy before)

On your client computer use the same above procedure to open the "Security Configuration" setting, make sure you check "Network DTC Access", "Allow Inbound/Outbound" option, restart service and computer if necessary.

On you SQL server service manager, click "Service" dropdown, select "Distribute Transaction Coordinator", it should be also running on your server computer.

+0

在弗洛林·拉扎后引用的DatabaseTransactionAdapter的伎俩。我将发布我的实现代码作为回应。 – 2008-11-26 15:44:01

2

在弗洛林·拉扎的DatabaseTransactionAdapter实施Keith Sirmons指出我似乎在做这个伎俩。这里是我的代码调用它:

Store.DBDataContext dc = new Store.DBDataContext(); 
using (TransactionScope transaction = new TransactionScope()) 
{ 
    try 
    { 
     var dbAdapter = new DatabaseTransactionAdapter(dc.Connection); 
     dc.Connection.Open(); 
     dbAdapter.Begin(); 
     dc.Transaction = (SqlTransaction)dbAdapter.Transaction; 
     Store.Product product = GetProduct("foo"); 
     dc.InsertOnSubmit(product); 
     dc.SubmitChanges(); 
     transaction.Complete(); 
    } 
    catch (Exception ex) 
    {     
     throw ex; 
    } 
} 

,这让我感到不安的唯一的事情是,我没有显式关闭,即使它不是一个“使用”语句内声明的连接。

但根据弗洛林拉扎尔,这是故意的。

And you also must not close the connection, because the connection should stay open until the transaction is completed, which happens after the “using” statement ends. The adapter will take ownership of the connection lifetime and close it when it is done with it.

0

更注意的是: - 服务器配置指南 启用网络COM +访问(在Windows Server 2003) 开始==>控制面板==>添加或删除程序==>添加/删除Windows组件,选择Application Server“,然后单击”详细信息“。单击启用网络COM +访问,然后单击确定。单击下一步,然后单击完成。
- 如果在2台服务器之间有防火墙,请在此范围端口上打开/关闭防火墙: 单击开始==>控制面板==>管理工具==>组件服务。展开组件服务,展开计算机,右键单击我的电脑,然后选择属性。 在我的电脑属性窗口中,单击选项卡默认协议,单击面向连接的TCP/IP并选择属性。在新窗口中,单击添加并键入新的DTC端口范围。 单击确定以应用这些更改。 服务器必须重新开始新的更改生效

1

的步骤在Windows 2008上启用此或更高版本是:

First verify the "Distribute Transaction Coordinator" Service is running on both database server computer and client computers

  1. Go to "Administrative Tools > Services"
  2. Turn on the "Distribute Transaction Coordinator" Service if it is not running

If it is running and client application is not on the same computer as the database server, on the computer running database server

  1. Go to "Administrative Tools > Component Services"
  2. On the left navigation tree, go to "Component Services > Computers > My Computer > Distributed Transaction Coordinator" (you may need to double click and wait as some nodes need time to expand)
  3. Right click on "Local DTC", select "Properties"
  4. Select "Security" tab
  5. Make sure you check "Network DTC Access", "Allow Remote Client", "Allow Inbound/Outbound"
  6. The service will restart
  7. BUT YOU MAY NEED TO REBOOT YOUR SERVER IF IT STILL DOESN'T WORK (This is the thing drove me crazy before)

On your client computer use the same above procedure to open the "Security Configuration" setting, make sure you check "Network DTC Access", "Allow Inbound/Outbound" option, restart service and computer if necessary.

On your SQL server service manager, click "Service" dropdown, select "Distribute Transaction Coordinator", it should be also running on your server computer.

相关问题