且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL Server 2008 R2中的死锁问题(.Net 2.0应用程序)

更新时间:2023-02-06 22:08:45

MSDN文章说明:


隔离级别具有连接范围,并且在使用SET TRANSACTION ISOLATION LEVEL语句为
连接设置后,
保持有效,直到连接被关闭或另一个隔离
级别设置。当连接关闭并返回到池时,
将保留最后一个SET TRANSACTION ISOLATION LEVEL
语句的隔离级别。重新使用池化
连接的后续连接使用在合并
连接时生效的隔离级别。

An isolation level has connection-wide scope, and once set for a connection with the SET TRANSACTION ISOLATION LEVEL statement, it remains in effect until the connection is closed or another isolation level is set. When a connection is closed and returned to the pool, the isolation level from the last SET TRANSACTION ISOLATION LEVEL statement is retained. Subsequent connections reusing a pooled connection use the isolation level that was in effect at the time the connection is pooled.

问题是连接被打开具有可序列化隔离级别;关联的事务已被处理,连接也已被处理,但连接未被销毁并转到连接池。下一次请求连接时(使用相同的连接字符串),这个连接被返回,并且由于查询未指定任何隔离级别,因此它在可序列化隔离级别中执行。

The issue was that a connection gets opened with Serializable isolation level; the associated transaction was disposed and so was the connection but the connection was not destroyed and went to the connection pool. Next time when a request for a connection was made (with same connection string) this very connection was getting returned and since the query did not specify any isolation level, it was executing in the Serializable isolation level.

基本上,如果你有一个连接池并在特定的隔离级别中打开一个连接,我们说Serializable,然后连接将返回到池,隔离级别设置为Serializable。下一次请求连接时,您不能确保不会返回此连接,因此即使通过默认隔离级别为ReadCommitted,您也可以获得这些可序列化连接之一。

Basically, if you have a connection pool and open a connection in a particular isolation level, let's say Serializable, then the connection will go back to the pool with the isolation level set to Serializable. Next time you request a connection, you can't be sure that this connection will not be returned so even through the default isolation level is ReadCommitted you may get one of these "Serializable" connections.

另一个警告是,每次设置隔离级别为Serializable(或任何其他事项),你可能会选择不同的连接,慢慢你可能污染越来越多

Another caveat is that each time you set the isolation level to Serializable (or anything else for that matter) you may be picking different connections and slowly you may pollute more and more connections in the connection pool by setting their isolation level to Serializable (or whatever you set).

我没有找到任何机制来重置连接池的连接(当它是在执行我的查询后回到连接池)。一种解决方法是显式重置每个连接的隔离级别。但这是单调乏味。

I didn't find any mechanism to reset the disposing connection (when it was going back to the connection pool after executing my query). One workaround is to explicitly reset the isolation level for each connection. But this is tedious.

因此,***替代方案是为不同的隔离级别创建单独的连接池