The disconnected model is the most used throughout the world, though not abusing on authentication over and over again.
Disconnected Mode
Here are some reason why you want to work disconnected most of the time:
- The number of CLA (Client Licences Agreements) bought for your database server (well, not really applicable here as it is MySQL);
- If too many people are connected at once, this slows down the DBE (Database Engine);
- Keeping connections opened keeps the network busy, and too busy network are more likely to fail;
- While a user is editing, let's say a customer details, he doesn't need to preserve his connection to the database server, and perhaps lock a row or a data table, which would result of big concurrent locking problems.
This code:
using(MySqlConnection conn = new MySqlConnection(connStr)) {
if (conn.State == ConnectionState.Closed)
try {
conn.Open();
} catch (MySqlException ex) {
// Exception handling here...
}
// Use of connection here...
}
The using keyword is used to automatically dispose objects instantiated within it as the article reference states:
Defines a scope, outside of which an object or objects will be disposed.
This way, you ensure that once the connection is no longer required, you dispoe of it. So yes, a new authentication will be required once this connection is instantiated again, as it no longer exists. There's a little polling done here for a short time, but this is nothing you need to worry about.
Connected Mode
In order to ensure the use of only one such connection throughout your application, you should use it as a singleton. However, once your connection string changes, someday, you'll have to ensure that all the application gets closed and reopen, so that this connection gets a refreshed connection string. That is not viable, but I don't know your context.
Using Enterprise Library Data Access Application Block
In order to bring your connection pool manageable, you may want to use the Enterprise Library Data Access Application Block.
The DAAB is an easy to use, fully configurable data access layer engineered by Microsoft engineers and other participating companies. Then, managing connection pool can be as easy as 1-2-3!
I think you could gain a lot using the DAAB which is fully configurable within XML file and requires very low maintenance.
EDIT If I may push a little further, I would perhaps consider using the Façade design pattern with Factories.
Using Facade and Factories Design Pattern Effectively
Having an "intelligent" façade, it is this that provide you with the connection you need. As such, here's a simple example (assuming you have a project setting called "DefaultConnectionString"):
public static class ApplicationFacade {
private static readonly ApplicationFactory _applicationFactory = new ApplicationFactory();
public static DefaultConnectionString {
get {
return Properties.Settings.Default.DefaultConnectionString;
}
}
public static IList<ICustomer> GetCustomers() {
using(var connection = OpenConnection())
_applicationFactory.GetCustomers(connection);
}
public MySqlConnection OpenConnection() {
var newConnection = new MySqlConnection(DefaultConnectionString);
try {
newConnection.Open();
} catch (Exception ex) {
// Exception handling...
}
return newConnection;
}
}
internal sealed class ApplicationFactory {
internal ApplicationFactory() {
}
internal IList<ICustomer> GetCustomers(MySqlConnection connection) {
if (connection.State != ConnectionState.Open)
throw new InvalidOperationException()
IList<ICustomer> customers = new List<ICustomer>();
var command = new MySqlCommand(connection, @"select * from Customers");
// Place code to get customers here...
return customers;
}
}
// So you'll be able to use share the same connection throught your factory whenever needed, preventing the overkill of authentication over and over again. Here's how this would be used:
public partial class MainForm : Form {
private void PopulateGrid() {
dataGridView1.DataSource = ApplicationFacade.GetCustomers();
// And you never care about the connection! All you want is the list of customers, that's all!
}
}
That is a pattern that I often use in my development projects. It allows one single entry point to my class libraries, and they're very easy to use.
Well, that's perhaps more than what you asked for, but I hope it helps.