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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:09:05+00:00 2026-06-09T17:09:05+00:00

An issue has arisen due to a code refactor, what is the best solution

  • 0

An issue has arisen due to a code refactor, what is the best solution in this case?

The issue is that the DbConnection was refactored from a local method variable into a class variable.
The application is multithreaded.
It appeared that the issue is with the DbConnection object being shared when it is a member variable.
What is the best solution? leave it as a local method varaible?

public IDataReader Execute(CommandBehavior behavior, string[] parameterNames, object[] arguments)
{

        DbConnection conn = null;
        try
        {

            conn = Connection.CreateConnection();

            DbCommand cmd = conn.CreateCommand();
            cmd.CommandText = StoredProcedureName;
            cmd.CommandType = CommandType.StoredProcedure;



           //     ..................................................

            // Perform the call.                        
            return DataCachingContext.SetCachedData(call, cmd.ExecuteReader(behavior));

        }
        catch (Exception ex)
        {
            //..........................
        }
        finally
        {
            //
        }

}

Just to clarify, here is the version which causes issue. with a runtime exception, issues with indexing into ResultSets, this is most likely due to the connection being overwritten.

DbConnection _conn = null;

public IDataReader Execute(CommandBehavior behavior, string[] parameterNames, object[] arguments)
{

        try
        {

            _conn = Connection.CreateConnection();

            DbCommand cmd = _conn.CreateCommand();
            cmd.CommandText = StoredProcedureName;
            cmd.CommandType = CommandType.StoredProcedure;



           //     ..................................................

            // Perform the call.                        
            return DataCachingContext.SetCachedData(call, cmd.ExecuteReader(behavior));

        }
        catch (Exception ex)
        {
            //..........................
        }
        finally
        {
            //
        }

}

After more investigation it looks like the DbConnection variable was made a class variable to enable unit testing. When it’s a local variable there’s no way to test it’s value. The state of the DbConnection was being tested

  • 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-06-09T17:09:06+00:00Added an answer on June 9, 2026 at 5:09 pm

    The application is multithreaded. It appeared that the issue is with the DbConnection object being shared when it is a member variable. What is the best solution? leave it as a local method varaible?

    Yes, a good pattern for using DbConnection is to create and dispose the connection in a method (storing it in local variable). Behind the scenes the connections are pooled so there is no significant overhead in doing it like this. You also avoid having to deal with shared state and locks.

    The code provided in the question does exactly what I describe here.

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

Sidebar

Related Questions

TL;DR Version : This question has arisen due to the fact that I have
I have a strange issue that has arisen recently: Whenever I enter text, even
so I am having this issue that has been driving me crazy for hours,
This issue has been on/off bugging me and I've written 4 wrappers now. I'm
This issue has me baffled, it's affecting a single user (to my knowledge) and
I know this issue has been touched on here but I have not found
Thanks for taking a look at this. The issue has to do with the
I'm working on a project, and I've come across an issue that has me
I have an issue which has arisen using Google Page Speed Online although I
EDIT: I found why it is doing this, but a question has now arisen

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.