I am hosting a WCF service which uses the mysql connector. For some reason once every 5-10 minutes I get an error that mysql is already in use. What should I do? Set it up s
Everytime you need to query your SQL server simply:
using (var conn = new MySqlConnection("Some connection string"))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "SELECT foo FROM bar";
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
// process the results
}
}
}
This ensures that connections are returned into the connection pool that ADO.NET manages for you and so that you don't get any risks of creating multiple connections that could slow things up. Also this code is perfectly reentrant and thus by definition thread safe => it could be executed concurrently from as many threads as you wish.