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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:59:58+00:00 2026-05-25T15:59:58+00:00

I am currently working on a C# (.NET) project which connects to a MySQL

  • 0

I am currently working on a C# (.NET) project which connects to a MySQL Community Server database and runs some queries. There are currently 4 classes which use their own MySQLConnection object (I’m using the MySQL .NET connector), in order to connect to the database.

Is this good practice, or should I use one ‘global’ (static?) connection? Using one single connection is kind of against my habits of structuring the code. Not a big fan of 100000 objects working with one shared static field. I bet it’s against most programmers’ views too.

I also noticed that once I call connection.Close(), the connection does not necessarily get closed. Trying to .Open() it again would result in an exception. I trust this is because I did not set “Pooling=False” in the connection string. I’ll experiment with it. If you have any thoughts on this one too, feel free to drop them here.

So, in short, I’d like some opinions on how to organize my MySQLConnections. If you think a single static MySQLConnection to be used by all of the program’s classes is better (not necessarily performance-wise, I’m talking more about the effect of multiple MySQLConnections on the database system itself), let me know why you think so.

I’m expecting for the final program to have around 10 or 15 classes, each actively querying the 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-25T15:59:58+00:00Added an answer on May 25, 2026 at 3:59 pm

    You can continue with one connection per class. Most ADO.NET providers use connection pooling per default as you have noticed. Close doesn’t really close the connection but return the connection to the pool. However, you should not try to Open() the connection again but create a new connection object.

    There is one downfall with using one connection per class and that’s transaction handling. Transactions can not be shared over multiple connections (unless you are using TransactionScope).

    I usually prefer one connection per “session” and take in the connection in the constructor to my repository classes. (As I usually use inversion of control containers). Google a bit about Unit Of Work implementations.

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

Sidebar

Related Questions

I am currently working on a .net project which I am dividing into different
I am currently working on a project in which I am using some divs,
I am currently working on a ASP.NET MVC 3 project in which I have
I am currently working on a ASP.NET MVC 3 project in which I need
I'm working on a project which currently uses NHibernate 2.1.2, FluentNHibernate 1.1 and Spring.Net
I am currently working on a project in .NET which consists of several logical
I am currently working on a VB.NET project and the hardest thing that I
I'm currently working on an C#/ASP.NET project that will host several differents e-commerce websites,
I'm currently working on a ASP.Net 3.5 project and trying to implement session timeout
The ASP.NET web app project I'm currently working on is getting slower over 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.