Question

I have a connection to a database on my asp.net page, where I am using the "using statement" many times. I am using it once for every method that uses the connection. My wish is to reduce these statements to only one, both for performance reasons (it feels unnecessary to start the connection over again on the same page) and because it makes code easier to read.

Is this idea a bad coding standard? What is the recommended way of doing this?
And if one using statement is enough - is there a good way to encapsulate the whole page within the using statement (not only in for example Page_Load)?

Was it helpful?

Solution 2

Your current approach is considered best practises. You should always use the using-statement on objects implementing IDisposable to dispose(+close) all umanaged resources as soon as possible. A method is a good scope for the using statement.

Note that if you use Connection-Pooling (default), you will not open/close connections when you call Open or Close. Instead you flag the connection as usable if you close it. So if you wouldn't close the connection, the connection-pool would not reuse this connection. Instead it would have to open a new physical connection which is very expensive. Once you reach the maximum connection count (def. 100) you'll even get an exception.

Connection-Pooling:

In practice, most applications use only one or a few different configurations for connections. This means that during application execution, many identical connections will be repeatedly opened and closed. To minimize the cost of opening connections, ADO.NET uses an optimization technique called connection pooling.

Connection pooling reduces the number of times that new connections must be opened. The pooler maintains ownership of the physical connection. It manages connections by keeping alive a set of active connections for each given connection configuration. Whenever a user calls Open on a connection, the pooler looks for an available connection in the pool. If a pooled connection is available, it returns it to the caller instead of opening a new connection. When the application calls Close on the connection, the pooler returns it to the pooled set of active connections instead of closing it. Once the connection is returned to the pool, it is ready to be reused on the next Open call.

OTHER TIPS

Opening and closing database connection frequently is a good practice. Keep your connection open till you require database access else close it as soon as possible.

few links:

Good practice to open/close connections in an asp.net application?

Best practice? open and close multi connections, or one large open connection for ado.net

Using Connection Pooling - MSDN

e.g

public static void GetConnObj()
    {
        using (SqlConnection con = new SqlConnection("yourConnectionString"))
        {
            con.Open();
            //other code here.
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top