Connections to SQL Server are automatically aggregated into an ASP.NET application: one pool for each individual connection string. If you follow the best practices and hide your database code in the DAL, the connection string of which is constant throughout the application, you will always work with one pool of connection objects.
So what does this mean for your approach to the database? Well, for one, this means that “closing the connection” really translates as “returning the connection to the pool”, and not really closing the application link to SQL Server. Thus, closing and reopening are not so important for the deal. However, with that said, there are several best practices.
First, you don’t want connections to end in your pool, even if your application scales dramatically. That is, never think in turn "user on this page" - think in terms of "thousands of people using this page."
Secondly, although it’s not the case to charge for closing and reopening the connection, you usually want to open the connection as late , using it until it is completed, and then close it as early as possible . The only exception is if you have a time-consuming process that must appear after some data is extracted and before other data is saved or received.
Thirdly, I would strongly advise you not to open the connection at the beginning of the page life cycle and close it at the end of the life cycle by another method. What for? Because the worst thing you can do is leave the connection open, because you forgot to add logic to close it. Yes, they will eventually be closed when the GC starts to work, but again, if you are thinking of “thousands of people using this page”, the likelihood of real problems becoming obvious.
Now, if you say that you are sure that you close the connection, because you always do it in some key logical place (for example, the Page_Unload method). Well, that’s great as long as you can confidently say that you will never throw an error that jumps out of the page life cycle. Which you cannot. Therefore ... do not open pages in one method of the life cycle and do not close in another.
Finally, I highly recommend implementing DAL, which manages database connections and provides tools for working with data. In addition, create a Business Logic Layer (BLL) that uses these tools to provide security type objects to the user interface (for example, Model objects). If you implement BLL objects with the IDisposable interface, you can always ensure the security of the connection using the scope. It will also allow you to keep the database connection open for a very short period of time: just open the BLL object, drag the data to a local object or list, and then close the BLL object (exit the scope). Then you can work with the data returned by the BLL after the connection has been closed.
So ... what it looks like. Well, on your pages (user interface) you will use the Business Logic classes as follows:
using (BusinessLogicSubClass bLogic = new BusinessLogicSubClass()) {
Your BusinessLogicSubClass will be obtained from the BusinessLogic object that implements IDisposable. It will instantiate your DAL class and open the connection as follows:
public class BusinessLogic : IDisposable { protected DBManagementClass qry; public BusinessLogic() { qry = new DBManagementClass(); } public void Dispose() { qry.Dispose(); <-- qry does the connection management as described below. } ... other methods that work with the qry class to ... retrieve, manipulate, update, etc. the data ... Example: returning a List<ModelClass> to the UI ... }
when the BusinessLogic class goes out of scope, the Dispose method will be called automatically because it implements the IDisposable interface.
Your DAL class will have the appropriate methods:
public class DBManagementClass : IDisposable { public static string ConnectionString { get; set; }
Given what I have described so far, DAL does not have to be IDisposable. However, I use my DAL class extensively in my test code when I test new BLL methods, so I built the DAL as IDisposable so that I can use the using construct during testing.
Follow this approach and, in essence, you will no longer have to think about a connection pool.