Home > Backend Development > C++ > How to Clean Up Expired SqlDependency Objects in SQL Server to Prevent Memory Leaks?

How to Clean Up Expired SqlDependency Objects in SQL Server to Prevent Memory Leaks?

Susan Sarandon
Release: 2025-01-12 13:01:47
Original
439 people have browsed it

How to Clean Up Expired SqlDependency Objects in SQL Server to Prevent Memory Leaks?

Addressing Expired SqlDependency Objects in SQL Server to Avoid Memory Leaks

The Issue:

SQL Server's SqlDependency objects persist in memory even after expiration, leading to escalating memory usage and potential server crashes. This article outlines how to proactively remove these lingering queries.

Resolution:

Even after employing SqlDependency.Stop(), remnants—conversation groups and endpoints—remain within the database. To rectify this, execute the following SQL script:

<code class="language-sql">DECLARE @ConvHandle uniqueidentifier
DECLARE Conv CURSOR FOR
SELECT CEP.conversation_handle FROM sys.conversation_endpoints CEP
WHERE CEP.state = 'DI' or CEP.state = 'CD'
OPEN Conv;
FETCH NEXT FROM Conv INTO @ConvHandle;
WHILE (@@FETCH_STATUS = 0) BEGIN
    END CONVERSATION @ConvHandle WITH CLEANUP;
    FETCH NEXT FROM Conv INTO @ConvHandle;
END
CLOSE Conv;
DEALLOCATE Conv;</code>
Copy after login

SqlDependency Shortcomings:

A key limitation of SqlDependency is its incomplete notification coverage for all table modifications during resubscription.

A Superior Alternative: SqlDependencyEx

The open-source SqlDependencyEx library leverages database triggers and Service Broker notifications to overcome SqlDependency's shortcomings. Here's an illustrative usage example:

<code class="language-csharp">int changesReceived = 0;
using (SqlDependencyEx sqlDependency = new SqlDependencyEx(
          TEST_CONNECTION_STRING, TEST_DATABASE_NAME, TEST_TABLE_NAME)) 
{
    sqlDependency.TableChanged += (o, e) => changesReceived++;
    sqlDependency.Start();

    // Simulate table modifications.
    MakeTableInsertDeleteChanges(changesCount);

    // Allow time for change reception.
    Thread.Sleep(1000);
}

Assert.AreEqual(changesCount, changesReceived);</code>
Copy after login

Employing SqlDependencyEx offers a more robust solution for tracking table changes while preventing the memory-related problems inherent in the standard SqlDependency class.

The above is the detailed content of How to Clean Up Expired SqlDependency Objects in SQL Server to Prevent Memory Leaks?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template