已经设置了隔离级别比较低了,可是做事务时还是死锁

ilearn 2013-08-01 01:25:32
下面是在第二个连接的时候就卡住了,也就是说在 connection2.Open();的时候卡住,不知道为什么
我的代码就是做个分布式事物,环境已经设好(MSDTC已经设好,并且DTCPING 都PING过,没问题),不知道卫什么回死锁,我的是测试环境, gjdwillupload 是个返型 LIST<T>,代码是查MSDN和往上的资料写的,请帮帮我,非常感谢,我环境是 192.168.0.25 是 WIN2K3+ SQL 2005,192.168.0.20是 WIN 2003+ SQL 2000

string[] connstr= new string[3];
connstr[0] = "server =192.168.0.25;database=db1;uid=sa;pwd=";
connstr[1] = "server =192.168.0.25;database=db2;uid=sa;pwd=";
connstr[2] = "server =192.168.0.20;database=db3;uid=sa;pwd=";

TransactionOptions tpt = new TransactionOptions();

tpt.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;

tpt.Timeout = new TimeSpan(0, 3, 0);

try
{
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, tpt))
{
using (SqlConnection connection1 = new SqlConnection(connstr[0]))
{
try
{
connection1.Open();

if (insertthegjd(connection1, 1, gjdwillupload, gjdmaindc)<0)// gjdwillupload 就是个 LIST<T>,gjdmaindc是个类
return 10;


using (SqlConnection connection2 = new SqlConnection(connstr[1]))
{
try
{

connection2.Open(); // 就在这卡住

if (insertthegjd(connection2, 2, gjdwillupload, gjdmaindc) < 0)
return 10;

using (SqlConnection connection3 = new SqlConnection(connstr[2]))
{
try
{
connection3.Open();

if (insertthegjd(connection3, 3, gjdwillupload, gjdmaindc) < 0)
return 10;


}
catch (Exception ex)
{
UploadthegjdFault upgjdf = new UploadthegjdFault();
upgjdf.Operation = "when insert the records";
upgjdf.ProblemType = "occur the exception";
string expmessag = ex.ToString();
if (expmessag.Length > 900)
expmessag = ex.ToString().Substring(0, 900);
logtheexp(ip, myusername, upgjdf.Operation + upgjdf.ProblemType + expmessag);
throw new FaultException<UploadthegjdFault>(upgjdf);
}

}


}




catch (Exception ex)
{
UploadthegjdFault upgjdf = new UploadthegjdFault();
upgjdf.Operation = "when insert the records";
upgjdf.ProblemType = "occur the exception ";
string expmessag = ex.ToString();
if (expmessag.Length > 900)
expmessag = ex.ToString().Substring(0, 900);
logtheexp(ip, myusername, upgjdf.Operation + upgjdf.ProblemType + expmessag);
throw new FaultException<UploadthegjdFault>(upgjdf);
}


}

}
catch (Exception ex)
{
UploadthegjdFault upgjdf = new UploadthegjdFault();
upgjdf.Operation = "when insert the records";
upgjdf.ProblemType = "insert exception happen";
string expmessag = ex.ToString();
if (expmessag.Length > 900)
expmessag = ex.ToString().Substring(0, 900);
logtheexp(ip, myusername, upgjdf.Operation + upgjdf.ProblemType + expmessag);
throw new FaultException<UploadthegjdFault>(upgjdf);
}




}










// The Complete method commits the transaction. If an exception has been thrown,
// Complete is not called and the transaction is rolled back.
scope.Complete();

return 1;
}
}
catch (TransactionAbortedException ex)
{
UploadthegjdFault upgjdf = new UploadthegjdFault();
upgjdf.Operation = "when the transaction is coming";
upgjdf.ProblemType = "insert exception";
string expmessag = ex.ToString();
if (expmessag.Length > 900)
expmessag = ex.ToString().Substring(0, 900);
logtheexp(ip, myusername, upgjdf.Operation + upgjdf.ProblemType + expmessag);
throw new FaultException<UploadthegjdFault>(upgjdf);
}
catch (ApplicationException ex)
{
UploadthegjdFault upgjdf = new UploadthegjdFault();
upgjdf.Operation = "when insert the records ";
upgjdf.ProblemType = "ocur exception";
string expmessag = ex.ToString();
if (expmessag.Length > 900)
expmessag = ex.ToString().Substring(0, 900);
logtheexp(ip, myusername, upgjdf.Operation + upgjdf.ProblemType + expmessag);
throw new FaultException<UploadthegjdFault>(upgjdf);
}

...全文
174 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ilearn 2013-08-01
  • 打赏
  • 举报
回复
发现就算我把插入记录的函数屏币了还是会大connect2.open()那里卡住了
ztszhq 2013-08-01
  • 打赏
  • 举报
回复
引用 3 楼 ilearn 的回复:
不行,还是不行
http://www.cnblogs.com/zhangpengshou/archive/2007/10/30/942670.html 看 COM+事务(分布式事务)
ilearn 2013-08-01
  • 打赏
  • 举报
回复
不行,还是不行
jshi123 2013-08-01
  • 打赏
  • 举报
回复
事务的默认隔离级别是Serializable,这意味着一个Connection打开时,另一个Connection会等待。 所以第二个Connection打开前要关闭前一个,像这样: connection1.Open(); if (insertthegjd(connection1, 1, gjdwillupload, gjdmaindc) < 0)// gjdwillupload 就是个 LIST<T>,gjdmaindc是个类 return 10; connection1.Close();
ilearn 2013-08-01
  • 打赏
  • 举报
回复
//下面是我的涵数 insertthegjd 的代码: public int insertthegjd(SqlConnection connection1, int thebelongto, Uploadthegjdsubver2List gjdwillupload, Uploadthegjdmaindcver2 gjdmaindc) { string clinozy = gjdmaindc.Myclinozymain; string clinonormal = gjdmaindc.Myclinomain; string listnozy = gjdmaindc.Mylistnomain.Substring(0, 14) + "d" + clinozy; string listnorecipe = gjdmaindc.Mylistnomain.Substring(0, 13) + "rd" + clinonormal; string listnozynormal=gjdmaindc.Mylistnomain.Substring(0, 13) + "zd" + clinonormal; int dbtotalcount = (from d in gjdwillupload where d.Mybelong == thebelongto select d.Myprodnosub).Count(); if (dbtotalcount > 0) { try { var query = (from d in gjdwillupload where d.Mybelong == thebelongto select new { d.Mylistnosub, d.Myreason }).Distinct(); foreach (var q in query) { string sqlinsmain = @"insert into gjd_main (list_no,cli_no,local_time,remote_time,belong_to,now_status) values (@mylistno,@myclino,@mylocaltime,@myremotetime,@mybelongto,@mynowstatus)"; SqlCommand cmdinsmain = connection1.CreateCommand(); cmdinsmain.CommandText = sqlinsmain; cmdinsmain.Parameters.Clear(); cmdinsmain.Parameters.Add("@mylistno", SqlDbType.Char, 19); cmdinsmain.Parameters.Add("@myclino", SqlDbType.Char, 4); cmdinsmain.Parameters.Add("@mylocaltime", SqlDbType.DateTime); cmdinsmain.Parameters.Add("@myremotetime", SqlDbType.DateTime); cmdinsmain.Parameters.Add("@mybelongto", SqlDbType.Int); cmdinsmain.Parameters.Add("@mynowstatus", SqlDbType.Int); cmdinsmain.Parameters["@mylistno"].Value = q.Mylistnosub; cmdinsmain.Parameters["@myclino"].Value = clinonormal; cmdinsmain.Parameters["@mylocaltime"].Value = gjdmaindc.Mylocaltimemain; DateTime dt = System.DateTime.Now; cmdinsmain.Parameters["@myremotetime"].Value = dt; cmdinsmain.Parameters["@mybelongto"].Value = 1; cmdinsmain.Parameters["@mynowstatus"].Value = gjdmaindc.Mynowstatusmain; cmdinsmain.ExecuteNonQuery(); } var query2 = from d in gjdwillupload where d.Mybelong == thebelongto select new { d.Mylistnosub, d.Myprodnosub, d.Myneednumsub, d.Mystatusub, d.Mylesttimesub }; foreach (var q2 in query2) { if (q2.Myprodnosub != null) { string sqlinssub = @"insert into gjd_sub (list_no,prod_no,need_num,status,lest_time) values (@mylistnosub,@myprodno,@myneednum,@mystatus,@mylesttime)"; SqlCommand cmdinssub = connection1.CreateCommand(); cmdinssub.CommandText = sqlinssub; cmdinssub.Parameters.Clear(); cmdinssub.Parameters.Add("@mylistnosub", SqlDbType.Char, 19); cmdinssub.Parameters.Add("@myprodno", SqlDbType.Char, 8); cmdinssub.Parameters.Add("@myneednum", SqlDbType.Decimal); cmdinssub.Parameters.Add("@mystatus", SqlDbType.Int); cmdinssub.Parameters.Add("@mylesttime", SqlDbType.DateTime); cmdinssub.Parameters["@mylistnosub"].Value = q2.Mylistnosub; cmdinssub.Parameters["@myprodno"].Value = q2.Myprodnosub; cmdinssub.Parameters["@myneednum"].Value = q2.Myneednumsub; cmdinssub.Parameters["@mystatus"].Value = q2.Mystatusub; cmdinssub.Parameters["@mylesttime"].Value = q2.Mylesttimesub; cmdinssub.ExecuteNonQuery(); } } return 1; } catch { throw; } } return 0; }

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧