Connection pooling in C # MySQL
I have an app C# multi threading
and using MySQL
with one connection to the whole app. But when two or more threads try to access the database at the same time, I get below error:
There is an already open
DataReader
one associated with thisConnection
, which should be closed first.
My connection code is below
public static _connectionSetup = new MySqlConnection("Server=server ; Database=database;User ID=user;Password=pass;Pooling=true;");
and when i need to use connection i use below code: -
using (MySqlConnection connection =_connectionSetup )
{
using (MySqlCommand command = new MySqlCommand("proc", connection))
{
....
}
}
I tried using pooling=true
and I created two seperated connections and also for two different threads, but still I get the above error.
Did I miss something?
How can I implement a connection pool so that the whole thread uses a separate connection and doesn't cause any problems?
source to share