Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6848571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:53:05+00:00 2026-05-27T00:53:05+00:00

It is clear that SqlConnections are pooled, so the using keyword seems perfect and

  • 0

It is clear that SqlConnections are pooled, so the using keyword seems perfect and this is the approach I always used and seen used.

For example…

public List<string> MyQuery()
{
    List<string> list = new List<string>();
    using (SqlConnection conn = new SqlConnection(connectionString))
    {
        conn.Open();
        // do some stuff with connection, process the resultset and fill the list.
    }
    return list;
}

The question is : with several queries for a webpage, is better to create the connection each time or to share the connection between queries, sharing it for an unit of work?

The performances will be much greater or is just a premature unnecessary microoptimization?

Is really important to close every connection as soon as possible or is better to try to pack the queries all together in an unit of work?

An example of an unit of work could be for example…

List<string> list1, list2;
string myvalue1, string myvalue2;

using (SqlConnection conn = new SqlConnection(connection))
{
    list1 = MyQuery1(conn);
    list2 = MyQuery2(conn);
    myvalue1 = MyQuery3(conn);
    myvalue2 = MyQuery4(conn);
}

This can happens during page load where multiple data should be get from database.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:53:06+00:00Added an answer on May 27, 2026 at 12:53 am

    Note that with connection pooling, you actually are not creating new connections with each ‘new’… you are generally just getting one back from the pool.

    Therefore, you should always open connections as late as you can, and close them as early as you can.

    If a set of commands needs to be part of one transaction, then it needs to be done together. But beyond that, I think you should always close when you are done, and get a new one.

    Even if you have no other code running between those commands… what if a thread context switch happens in between? Your connection object is needlessly open, doing nothing, waiting to get control back.

    If you had closed it, it could have gone back to the pool.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find it annoying that I can't clear a list. In this example: a
First of all, I'm using Windows, just to make that clear. I am using
It seems that everybody knows you're supposed to have a clear distinction between the
Python is so dynamic that it's not always clear what's going on in a
Edit 2: It seems clear that no one seems to be able to understand
First of all I am making it clear that, I am not using any
It's clear that you should be using exceptions to handle errors in your app.
It seems pretty clear that it is supposed to set things up. When exactly
Edited again because it originally wasn't clear that I'm trying to initialize the arrays
As I put together each asp.net page It's clear that most of the time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.