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 7501587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:35:19+00:00 2026-05-29T20:35:19+00:00

I was looking at some code and discussing it with co-workers. Specifically a section

  • 0

I was looking at some code and discussing it with co-workers.

Specifically a section of code that looks like this.

    [Test]
    public void TestNormalWay()
    {
        using(var cn = GetConnection())
        {
            cn.Open();
            // do stuff
        }
    }

The question came up:

“why not move the cn.Open into the GetConnection method.”

I said that if “Open” throws an exception dispose would not get called. His response was

“So what. The connection wasn’t opened so why would it need to get
closed (or disposed)?”

For me it is just a matter of not wanting to know if or if not I NEED to dispose/close so I would repeat the cn.Open in the code instead of moving it into the shared function.

BUT it is interesting… so I did some reading at SQL Server Connection Pooling (ADO.NET)

To me it isn’t clear if there exists a scenario in which calling cn.Open and it throws and exception where dispose would need to be called.

So in my example below is there any difference really between “TestNormalWay” and “WhyNotDoItThisWay”

    protected static DbConnection GetConnection()
    {
        DbConnection cn = new SqlConnection("SomeConnecitonstring... ");
        return cn; 
    }

    protected static DbConnection GetConnectionDangerousVersion()
    {
        DbConnection cn = new SqlConnection("SomeConnecitonstring... ");
        cn.Open();  // this will throw.. .dispose not called
        return cn; 
    }

    [Test]
    public void TestNormalWay()
    {
        using(var cn = GetConnection())
        {
            cn.Open();
            // do stuff
        }
    }

    [Test]
    public void WhyNotDoItThisWay()
    {
        using(var cn = GetConnectionDangerousVersion())
        {
            // do stuff
        }
    }
  • 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-29T20:35:20+00:00Added an answer on May 29, 2026 at 8:35 pm

    The way you’re writing your code you always want to open the connection as soon as it’s created so there is no difference.

    However you can open and close a connection several times and in code designed to do that there is a big difference.

    I might want to write some code where I have a long running routine that takes a connection object and over time opens and closes it. The routine might not care how the connection object was created. Therefore it’s an advantage to separate the act of creating the connection with the act of opening and closing it.

    With regards the resource management question I’d agree it’s not an issue. Creating an SQL connection object in itself doesn’t lock any resources, it’s the act of opening it that acquires a pooled connection. If the open returns an exception I think it’s reasonable to assume that the connection wasn’t opened.

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

Sidebar

Related Questions

So I'm looking through some code, and I see this: class whatever { public:
Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like
Looking at some code I'm maintaining in System Verilog I see some signals that
I am looking at some code and it has this statement: ~ConnectionManager() { Dispose(false);
I'm looking through some code for learning purposes. I'm working through this portion of
While looking over some code, I found a function that seems to do exactly
I am looking over some code someone else did and I see this: if
I was looking through some code the other day and I saw something like
Looking at some code I noticed that another dev had changed every instance of
Looking at some code, learing about threads : import java.applet.*; import java.awt.*; public class

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.