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 thisConnection
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?
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.
The main difference between C and C++ is that C is a procedural programming language that does not support classes and objects. On the other hand, C++ is an extension of C programming with object-oriented programming (OOP) support.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With