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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:44:43+00:00 2026-05-11T01:44:43+00:00

I’m wondering what would be the best prectice regarding mainataining connections to the database

  • 0

I’m wondering what would be the best prectice regarding mainataining connections to the database in .Net application (ADO.NET but I guess the practice should be the same for any data layer). Should I create a database connection and propagate it throughout my application, or would it be better to just pass connection strings/factories and create a connection ad-hoc, when it is needed.

As I understand perfomance hit is not signifcant with pooling and it allows me to recover from broken connections quite easily (just a new connection will be created) but then again a connection object is a nice, relatively high-level abstraction and creating a new connection for every operation (not SQL command, but application operation) generates additional, duplicated code and feels like a waste of time/resources(?).

What do you think about these 2 cases, what are their cons/pros and which approach are you using in your real-life applications?

Thanks

  • 1 1 Answer
  • 2 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. 2026-05-11T01:44:44+00:00Added an answer on May 11, 2026 at 1:44 am

    I found myself needing to pass around a connection object so I could allow several business objects to save themselves to the database inside a single transaction.

    If each business object had to create its own SQLConnection to the database, the transaction would escalate to a distributed transaction and I wanted to avoid that.

    I did not like having to pass the SQLConnection object as a parameter to save an object, so I created a ConnectionManager that handles creating the SQLConnection object for me, tracking the use of the SQLConnection object, and disconnecting the SQLConnection object when not in use.

    Here is some code as an example of the ConnectionManager:

    public class ConnectionManager: IDisposable {     private ConnectionManager instance;      [ThreadStatic]     private static object lockObject;      private static Object LockObject     {         get         {             if (lockObject == null)                 lockObject = new object();             return lockObject;         }     }      [ThreadStatic]     private static Dictionary<string, ConnectionManager> managers;     private static Dictionary<string, ConnectionManager> Managers     {         get         {             if (managers == null)                 managers = new Dictionary<string, ConnectionManager>();             return managers;         }     }      private SqlConnection connection = null;     private int referenceCount;     private string name;       public static ConnectionManager GetManager(string connectionName)     {         lock (LockObject)         {             ConnectionManager mgr;             if (Managers.ContainsKey(connectionName))             {                 mgr = Managers[connectionName];             }             else             {                 mgr = new ConnectionManager(connectionName);                 Managers.Add(connectionName, mgr);             }              mgr.AddRef();             return mgr;         }     }      private ConnectionManager(string connectionName)     {         name = connectionName;         connection = new SqlConnection(GetConnectionString(connectionName));         connection.Open();     }      private string GetConnectionString(string connectionName)     {         string conString = Configuration.ConnectionString;         return conString;      }      public SqlConnection Connection     {         get { return connection; }     }      private void AddRef()     {         referenceCount += 1;     }      private void DeRef()     {         lock (LockObject)         {             referenceCount -= 1;             if (referenceCount == 0)             {                 connection.Dispose();                 Managers.Remove(name);             }         }     }  #region IDisposable Members      public void Dispose()     {         Dispose(true);     }      protected virtual void Dispose(bool disposing)     {         if (disposing)         {             DeRef();         }     }      ~ConnectionManager()     {         Dispose(false);     }  #endregion  } 

    Here is how I would use it from a business object:

    public void Save() {        using (ConnectionManager mrg = ConnectionManager.GetManager('SQLConnectionString')     {         using (SQLCommand cmd = new SQLCommand)         {             cmd.connection = mgr.Connection             // More ADO Code Here         }          _childObject.Save(); //this child object follows the same pattern with a using ConnectionManager.     } } 

    I save a business object and all of its children are saved as well using the same connection object. When the scope falls away from original parent, the using statement closes the connection.

    This is a pattern I learned from Rocky Lhotka in his CSLA framework.

    Keith

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string
This could be a duplicate question, but I have no idea what search terms
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I

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.