The root cause of your error is that (hoping to avoid the obvious), the same connection is being used, and already open/executing. Are they static
at the moment? Suggest looking into why the pooling isn't being used or is being limited.
Consider refactoring your code to have using
statements. Pseudo, noncompiling example:
private void ExecNonQuery(string sqlStatement)
{
using (var conn = new MySqlConnection(connString))
{
using (var cmd = new MySqlDataCommand(conn))
{
cmd.ExecuteNonQuery(sqlStatement);
}
}
}
or
private List<Customer> GetCustomers( )
{
string sqlStatement = "SELECT ID, Name FROM Customer";
using (var conn = new MySqlConnection(connString))
{
using (var cmd = new MySqlDataCommand(conn))
{
//get a reader and do something
using (var reader = new MySqlDataReader(sqlStatement))
{
...
}
}
}
}
By wrapping the creation of those objects in a using
statement, you'll be ensuring that your objects are closed and disposed of properly.
I'd suggest that yes, you should be opening a new connection everytime. The ADO.NET Driver for MySQL should use proper connection pooling. You really shouldn't have to think about it.
Why have multiple processes share the same connection? What's the need there?