Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a library written in C # that allows me to access the sql server, this library exposes different properties and methods for its use. The library has been implemented (rewritten) starting from a COM-EXE library that does the same things written in VB6.
I noticed that on the server sql remained active many connections, even a few days ago, even if at the end of the use of the library, by the processes that use it, the imposed to NULL.

Where am I doing wrong. I'm afraid that all these useless connections can only reduce the performance of the sql server.

What I have tried:

I also implemented the IDisposable interface in the library to set all the objects declared private to NULL but without the result being changed.
Posted
Updated 19-Dec-17 0:52am
Comments
Jochen Arndt 19-Dec-17 4:19am    
This can't be answered without seeing code.

Ensure that every connection is closed when no longer used. This can be done by calling either Close() or Dispose() or with a Using block.

Setting to NULL does not Dispose an object - it just removes the reference from use which allows the Garbage Collector to clean it up when it runs - which will only occur normally when the memory in the system starts to run low. As a result, unless you explicitly close (and Dispose) your SqlConnection (and SqlCommand) instances, they remain active, probaboly until your app closes.

The best way to handle this is to use using blocks:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT Id, description FROM myTable", con))
        {
        using (SqlDataReader reader = cmd.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int) reader["Id"];
                string desc = (string) reader["description"];
                Console.WriteLine("ID: {0}\n    {1}", id, desc);
                }
            }
        }
    }
This will ensure that each object is automatically Closed and Disposed when it goes out of scope.
 
Share this answer
 
This is just connection pooling. Connections are expensive to create so when your app closing a connection it isn't actually closed, it is just put in the pool to be reused so the next time a connection is opened it will get one from the pool if available, and when closed it is put back in the pool. That way your app doesn't have to worry about creating\closing connections. So rather than being bad for performance it is actually good for performance and nothing you should worry about.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900