Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
what happen if i do not close sql connection in asp.net c#? but still data transaction are working successfully.?
Posted

Depends. You could run out of connections, you could have difficulty creating a new DataReader - it depends on how the SQL server is configured, and how your code works.

Certainly, if an object implements IDisposable, you should always Dispose the object when you are finished with it - and that applies to SqlConnections, SQLCommands and so forth.

In the case of SqlConnection objects, Dispose calls Close foe you, so it's the easiest way.
 
Share this answer
 
Comments
adriancs 31-Dec-13 3:05am    
If I understand correctly, after an ASP.NET page life cycle ends, IIS will release/dispose every object called within the thread, and this subsequently will close and dispose SqlConnection too. But I agree that, we should close the connection as soon as possible.
If you open the connection and don't close it, then it would decrease the connection pools and limits available for connecting to database again.

It is always recommended to close the connection and data reader objects explicitly when you use them.
 
Share this answer
 
If the number of concurrent users increase then perhaps you will run out of connections. I have spent many night fixing this problem in old legacy(inherited code).

Perhaps this article will help you understand the problem and provide some best practices for connection handling: ASP.NET - How To Use(Open/Close) Connections Correctly[^]
 
Share this answer
 
You may get an error like this after some executions

"Timeout expired.  The timeout period elapsed prior to obtaining a connection from the pool.  This may have occurred because all pooled connections were in use and max pool size was reached."


This may have occurred because all pooled connections were in use and max pool size was reached.
(default pool size is 100)

so u must close connections as soon as possible like,

SqlCommand cmd = new SqlCommand(str, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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