TransactionScope Troubleshooting
2010-01-18 11:07
267 查看
只要涉及到数据库的操作,那么使用事务就是难免的。如果我们使用LINQtoSQL作为数据访问层,那么LINQ提供的SubmitChanges()方法自身就包含了对事务的处理。当然,我们也可以利用System.Data.Common.DbTransaction对事务进行处理,我们可以调用DataContext中Connection的方法BeginTransaction()启动事务,然后根据情况进行回滚或提交。例如是这样一段代码:
然而,当我们在使用LINQtoSQL中时,往往会同时使用多个DataContext,此时我们就需要使用TransactionScope。例如:
代码中,分别涉及到Update,Delete等操作,因此我们势必需要用事务,保证数据整体提交或整体回滚。在使用事务的时候,有一些前置条件是必备的。例如启动DistributedTransactionCoordinator服务,否则,就会抛出System.Data.SqlClient.SqlException异常,信息为:"MSDTConserver'{ServerName}'isunavailable."。是的,很多资料都是这样描述的。然而,现实并没有这么简单。我们首先得考虑运行代码的机器是否与数据库所在的机器是同一台。这里所谓的启动DistributedTransactionCoordinator服务,实际上是要启动数据库服务器的服务。如果数据库与代码服务器是同一台,通过这样的设置就没有错误了。
当数据库与代码服务器分属两台机器呢?同样运行如上的代码,就会抛出System.Transactions.TransactionManagerCommunicationException异常。异常信息为:"NetworkaccessforDistributedTransactionManager(MSDTC)hasbeendisabled.PleaseenableDTCfornetworkaccessinthesecurityconfigurationforMSDTCusingtheComponentServicesAdministrativetool."
这是一种通信错误,原因在于两台服务器之间的安全配置禁止了分布式事务。解决办法是在运行代码的服务器上,配置ComponentServices。方法如下:
1、在Run运行窗口中,输入dcomcnfg命令,这样就可以打开ComponentServices。
2、选择ComponentServices->Computers->MyComputer;
3、右键单击MyComputer,在弹出的快捷菜单中,选择“Properties”,然后点击MSDTCtab;
4、在MSDTCtab中,点击SecurityConfiguration按钮;
5、在弹出的对话框中参照下表的建议进行设置:
最后的设置如截图:
如果操作系统是Windows2003,通常默认的设置就是正确的。不过我们在编写程序时,不管是UnitTest,还是其他测试,最频繁的还是在本机上运行。如果操作系统是WindowsXP,就不得不进行这样的设置了。
注:此贴中没写在防火墙中加入msdtc.exe,值得商榷
LinqSampleDataContextcontext=newLinqSampleDataContext(); System.Data.Common.DbTransactiontrans=null; try { context.Connection.Open(); trans=context.Connection.BeginTransaction(); context.Transaction=trans; context.Employees.InsertOnSubmit(emp); context.SubmitChanges(); trans.Commit(); } catch(Exceptionex) { if(trans!=null) { trans.Rollback(); } }
然而,当我们在使用LINQtoSQL中时,往往会同时使用多个DataContext,此时我们就需要使用TransactionScope。例如:
using(TransactionScopescope=newTransactionScope(TransactionScopeOption.RequiresNew))
{
try
{
for(inti=0;i<nominees.Count;++i)
{
BackupnewBackup=nominees[i];
Ticketticket=tickets[i];
//updatetheinformationofticket
//mainlyaddtheinformationofemployee;
ticket.EmployeeID=newBackup.EmployeeID;
ticket.HaveNominated=true;
ticket.IsConfirmedByManager=true;
ticket.Status=TicketStatus.Enroll.ToString();
ticketAccessor.Update(ticket);
}
//updatetheIsSubmitofbackup;
ChangeSubmitStatue(backup);
//removetherecordofnomineeinbackuptable
Delete(nominees);
}
catch(Exceptionex)
{
ThrowHelper.ThrowBackupException("Finalizingoccursanerror.Thetranscationwillberollback.");
returnfalse;
}
scope.Complete();
}
代码中,分别涉及到Update,Delete等操作,因此我们势必需要用事务,保证数据整体提交或整体回滚。在使用事务的时候,有一些前置条件是必备的。例如启动DistributedTransactionCoordinator服务,否则,就会抛出System.Data.SqlClient.SqlException异常,信息为:"MSDTCon
当数据库与代码服务器分属两台机器呢?同样运行如上的代码,就会抛出System.Transactions.TransactionManagerCommunicationException异常。异常信息为:"NetworkaccessforDistributedTransactionManager(MSDTC)hasbeendisabled.PleaseenableDTCfornetworkaccessinthesecurityconfigurationforMSDTCusingtheComponentServicesAdministrativetool."
这是一种通信错误,原因在于两台服务器之间的安全配置禁止了分布式事务。解决办法是在运行代码的服务器上,配置ComponentServices。方法如下:
1、在Run运行窗口中,输入dcomcnfg命令,这样就可以打开ComponentServices。
2、选择ComponentServices->Computers->MyComputer;
3、右键单击MyComputer,在弹出的快捷菜单中,选择“Properties”,然后点击MSDTCtab;
4、在MSDTCtab中,点击SecurityConfiguration按钮;
5、在弹出的对话框中参照下表的建议进行设置:
ConfigurationOption | DefaultValue | RecommendedValue |
---|---|---|
NetworkDTCAccess | Disabled | Enabled |
ClientandAdministration | ||
AllowRemoteClients | Disabled | Disabled |
AllowRemoteAdministration | Disabled | Disabled |
TransactionManagerCommunication | ||
AllowInbound | Disabled | Enabled |
AllowOutbound | Disabled | Enabled |
MutualAuthenticationRequired | Enabled | EnabledifallremotemachinesarerunningWin2K3SP1orXPSP2orhigher,andareconfiguredwith“MutualAuthenticationRequired”. |
IncomingCallerAuthenticationRequired | Disabled | EnabledifrunningMSDTConcluster. |
NoAuthenticationRequired | Disabled | Enabledifremotemachinesarepre-WindowsServer2003SP1orpre-WindowsXPSP2. |
EnableTIP | Disabled | EnabledifrunningtheBAMPortal. |
EnableXATransactions | Disabled | EnabledifcommunicatingwithanXAbasedtransactionalsystemsuchaswhencommunicatingwithIBMWebSphereMQusingtheMQSeriesadapter. |
如果操作系统是Windows2003,通常默认的设置就是正确的。不过我们在编写程序时,不管是UnitTest,还是其他测试,最频繁的还是在本机上运行。如果操作系统是WindowsXP,就不得不进行这样的设置了。
注:此贴中没写在防火墙中加入msdtc.exe,值得商榷
相关文章推荐
- TransactionScope Troubleshooting
- TransactionScope Troubleshooting
- Troubleshooting on TransactionScope
- First step in troubleshooting complex issues: Define and scope your issue properly
- Master Note: Troubleshooting Database Transaction Recovery [ID 1494886.1]
- Interlude: Troubleshooting for 'no corresponding BEGIN TRANSACTION'
- Master Note: Troubleshooting Database Transaction Recovery (Doc ID 1494886.1)
- Downlink Throughput Troubleshooting
- .Net TransactionScope事务
- Troubleshooting Network Teaming Problems in ESX/ESXi
- 《Troubleshooting SQL Server》读书笔记-磁盘I/O配置
- 《Troubleshooting SQL Server》读书笔记
- oracle 监听的trouble-shooting
- XenAPP Troubleshooting XML Service Issues (WI账户无法登陆,报错)
- Spark Streaming Job Troubleshooting of Dependency Chain
- Troubleshooting Lync FOR MAC client issue
- Linux 服务器故障排除的前五分钟 (First 5 Minutes Troubleshooting A Linux Server)
- * Troubleshooting Performance Issues (文档 ID 1377446.1)
- Tempdb--monitoring and troubleshooting
- Windows Server Troubleshooting: "The RPC server is unavailable"