In my application i'm using Mysql.Data connector from dev.mysql.com to write into my mysql database, and i want to maximize performance, so i don't want to open many MySql connections. But eventually when i tried to use one global static connection like so:
MySqlConnection connect = new MySqlConnection(connectionString);
connect.Open();
StaticData.mysqlConnect = connect;
...
// in my threads
lock (StaticData.mysqlConnect)
{
foreach (Object param in records)
{
using (MySqlCommand command = conn.CreateCommand())
{
command.CommandText = "some request";
//...
command.ExecuteNonQuery();
}
}
}
I got System.Threading.SynchronizationLockException. So my question is: whats the proper way to share one connection between different threads using dev mysql connector?