TransactionScope Troubleshooting
 LinqSampleDataContext context = new LinqSampleDataContext();
LinqSampleDataContext context = new LinqSampleDataContext();             System.Data.Common.DbTransaction trans = null;
System.Data.Common.DbTransaction trans = null; try
try {
{ context.Connection.Open();
   context.Connection.Open(); trans = context.Connection.BeginTransaction();
   trans = context.Connection.BeginTransaction(); context.Transaction = trans;
   context.Transaction = trans;
 context.Employees.InsertOnSubmit(emp);
   context.Employees.InsertOnSubmit(emp); context.SubmitChanges();
                context.SubmitChanges();
 trans.Commit();
   trans.Commit(); }
} catch (Exception ex)
catch (Exception ex) {
{ if (trans != null)
   if (trans != null) {
   { trans.Rollback();
       trans.Rollback(); }
   } }
}然而,当我们在使用LINQ to SQL中时,往往会同时使用多个DataContext,此时我们就需要使用TransactionScope。例如:
 using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
        using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew)) {
        { try
            try {
            {
 for (int i = 0; i < nominees.Count; ++i)
                for (int i = 0; i < nominees.Count; ++i) {
                { Backup newBackup = nominees[i];
                    Backup newBackup = nominees[i]; Ticket ticket = tickets[i];
                    Ticket ticket = tickets[i];
 //update the information of ticket
                    //update the information of ticket //mainly add the information of employee;
                    //mainly add the information of employee; ticket.EmployeeID = newBackup.EmployeeID;
                    ticket.EmployeeID = newBackup.EmployeeID; ticket.HaveNominated = true;
                    ticket.HaveNominated = true; ticket.IsConfirmedByManager = true;
                    ticket.IsConfirmedByManager = true; ticket.Status = TicketStatus.Enroll.ToString();
                    ticket.Status = TicketStatus.Enroll.ToString();
 ticketAccessor.Update(ticket);
                    ticketAccessor.Update(ticket); }
                }
 //update the IsSubmit of backup;
                //update the IsSubmit of backup; ChangeSubmitStatue(backup);
                ChangeSubmitStatue(backup);
 //remove the record of nominee in backup table
                //remove the record of nominee in backup table Delete(nominees);
                Delete(nominees); }
            } catch (Exception ex)
            catch (Exception ex) {
            { ThrowHelper.ThrowBackupException("Finalizing occurs an error. The transcation will be rollback.");
                ThrowHelper.ThrowBackupException("Finalizing occurs an error. The transcation will be rollback."); return false;
                return false; }
            }
 scope.Complete();
            scope.Complete(); }
        }当数据库与代码服务器分属两台机器呢?同样运行如上的代码,就会抛出System.Transactions.TransactionManagerCommunicationException异常。异常信息为:"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."
这是一种通信错误,原因在于两台服务器之间的安全配置禁止了分布式事务。解决办法是在运行代码的服务器上,配置Component Services。方法如下:
1、在Run运行窗口中,输入dcomcnfg命令,这样就可以打开Component Services。
2、选择Component Services->Computers->My Computer;
3、右键单击My Computer,在弹出的快捷菜单中,选择“Properties”,然后点击MSDTC tab;
4、在MSDTC tab中,点击Security Configuration按钮;
5、在弹出的对话框中参照下表的建议进行设置:
| Configuration Option | Default Value | Recommended Value | 
|---|---|---|
| Network DTC Access | Disabled | Enabled | 
| Client and Administration | 
 | 
 | 
| Allow Remote Clients | Disabled | Disabled | 
| Allow Remote Administration | Disabled | Disabled | 
| Transaction Manager Communication | 
 | 
 | 
| Allow Inbound | Disabled | Enabled | 
| Allow Outbound | Disabled | Enabled | 
| Mutual Authentication Required | Enabled | Enabled if all remote machines are running Win2K3 SP1 or XP SP2 or higher, and are configured with “Mutual Authentication Required”. | 
| Incoming Caller Authentication Required | Disabled | Enabled if running MSDTC on cluster. | 
| No Authentication Required | Disabled | Enabled if remote machines are pre-Windows Server 2003 SP1 or pre- Windows XP SP2. | 
| Enable TIP | Disabled | Enabled if running the BAM Portal. | 
| Enable XA Transactions | Disabled | Enabled if communicating with an XA based transactional system such as when communicating with IBM WebSphere MQ using the MQSeries adapter. | 

 
                    
                

 
    
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号