C#种死锁:事务(进程ID112)与另⼀个进程被死锁在锁通信缓冲区资源上,并且已被选作死锁。。。
如果您的程序报如下错误,那么说明你的程序中多个资源同时操作某张表,竞争⼒若的需要等待竞争⼒强的资源,等待释放后才能操作,导致数据库死锁:System.Data.SqlClient.SqlException (0x80131904): 事务(进程 ID 112)与另⼀个进程被死锁在锁 | 通信缓冲区资源上,并且已被选作死锁牺牲品。请重新运⾏该事务。
在 System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
在 System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
在 System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout)
在 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
在 System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
在 Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteNonQuery(SqlConnection connection, CommandType commandType, String commandText, SqlParameter[] commandParameters)
在 Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteNonQuery(String connectionString, CommandType commandType, String commandText, SqlParameter[] commandParameters)
死锁是不可避免的,但是我们要竟可能的避免数据库死锁,解决⽅法如下:
sql数据库迁移另一个硬盘
下列⽅法有助于最⼤限度地降低死锁:
1.按同⼀顺序访问对象
2.尽量能减少就减少数据库操作次数
3.如果某⼀个动作响应太慢,可使⽤多线程操作,来提⾼速度
4.添加try catch 异常捕捉机制,如果发⽣死锁,肯定会抛出异常,在异常种 sleep⼏秒后重新执⾏该过程

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。