C# MySQL Connection Pooling

I am having C# multi threading application and using MySQL with single connection to whole application. But when two or more thread try to access database at the same time, then i get below error :

There is already an open DataReader associated with this Connection which must 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 am using below code :-

using (MySqlConnection connection =_connectionSetup )
{
    using (MySqlCommand command = new MySqlCommand("proc", connection))
    {
        ....
    }
}

I tried used pooling=true and i have create two separated connection as well for two different thread, but still i am getting above error.
Am I missing something?

How can I implement connection pool so that all thread will use separate connection and won’t cause any issue?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Pooling is turned on by default, so you don’t need that connection string parameter.

Don’t share MySqlConnection instances. That’s it.

Pooling is not something you implement in your code, it’s done for you by ADO.NET.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x