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

  • Home
  • SEARCH
  • 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 528975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:02:35+00:00 2026-05-13T09:02:35+00:00

Is it acceptable to cache an instance of the database connection on application start?

  • 0

Is it acceptable to cache an instance of the database connection on application start?

Looking at the MSDN documentation on thread safety, I quote:

Any public static […] members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Given that, is it acceptable/safe for code such as this example below:

public static class BookingMapper
{
  public static Database Db { get; set; }

  static BookingMapper()
  {
    Db = DatabaseFactory.CreateDatabase();
  }

  public static string GetBooking(int id)
  {
    using (DbCommand cmd = Db.GetStoredProcCommand("getBooking"))
    {
      Db.AddInParameter(cmd, "@Id", DbType.Int32, id);
      using (IDataReader dr = Db.ExecuteReader(cmd))
      {
        ...
      }
    }
  }
}

If it is acceptable, what are the benefits/drawbacks of using such an approach over simply instantiating the Database on every single method call?

Thanks in advance.

Update:

Further research has pointed me to a PrimaryObjects.com article which, in the Putting the Database Factory to Use section suggests that this is acceptable. But I’m still wondering if there are pros/cons to doing it this way?

Similar question

  • 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-13T09:02:35+00:00Added an answer on May 13, 2026 at 9:02 am

    1) There are two ways to interpret that standard phrase on Thread Safety from MSDN, and I wish they’d clarify it. Your interpretation would be nice, but I believe that what it means is that:

    Any members (methods, fields, properties, etc) that are part of this type, and that are public and static, are thread safe

    (e.g. there are two ways to interpret the subphrase “members of this type”)

    2) Generally, you don’t want to share a db connection around – you want to open a connection, do your job, and close it. You can’t generally have multiple open readers associated with a single connection (this is generic db/connection advice, not ent library specific).

    3) On some further reading inside the ent library, the Database object returned by the CreateDatabase call isn’t a connection itself, and it looks like the connection management is handled as I stated in point 2. So it looks like the Database object itself can be safely shared around.

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

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.