已经设置了隔离级别比较低了,可是做事务时还是死锁
下面是在第二个连接的时候就卡住了,也就是说在 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);
}
我的代码就是做个分布式事物,环境已经设好(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);
}
...全文
相关推荐
SQLServler自动杀死死锁进程
**自动杀死死锁进程**: SQL Server可以通过设置死锁超时和死锁优先级来自动处理死锁。
MySQL事务及Spring隔离级别实现原理详解
正确设置事务隔离级别可以平衡数据一致性与并发性能之间的需求,而处理好死锁问题则能保证系统的稳定性。在实际应用中,开发者应根据业务场景选择合适的事务隔离级别,并避免可能导致死锁的操作。
mysql死锁的一些案例
设置合适的`innodb_lock_wait_timeout`,及时回滚死锁事务。
mysql死锁解决
**设置合理的超时时间**:为事务设置合理的超时时间,超时后自动回滚。4. **调整事务隔离级别**:适当降低事务的隔离级别可以在一定程度上减少死锁发生的概率。5.
Mysql 数据库死锁过程分析(select for update)
另外,可以通过设置合适的隔离级别和事务超时来减少死锁的可能性。总之,理解`SELECT ... FOR UPDATE`的工作原理以及InnoDB存储引擎的行锁机制对于识别和解决死锁至关重要。


