Understanding how SqlConnection opens and closes connections

by sunil ravulapalli /15. October 2013 07:49 /.net /Comments (0)
When you do conn.open() IIS opens a physical connection to DB. 
When u do conn.close() IIS moves to the connection pool. 
If some request is made using the same connection string IIS will use it. 
In the same app if you do:
conn.open()
conn.close()
and then  
conn1.open() 
conn1.close()
IIS will use the same physical connection.
But if you do: 
conn1.open() 
conn2.open()
IIS will open two physical connections. 
You can return the 2 connections to pool by doing
conn1.close() 
conn2.close()
 
If you do not close connections conn1 and conn2 will remain open for a long time and wont return to connection pool. 
So if there are new connection like conn3 and conn4, IIS will have a total 4 physical connections. If your website hits traffic on black friday, there will be hundreds of such connections which will crash your site :).
blog comments powered by Disqus