我正在尝试在 MySQL 的 .net 连接器 6.6.4.0 上使用连接池来使用以下代码为多个线程提供服务:
static public uint uNonQuery(string query)
{
using (Connection = new MySqlConnection(ConnectionString))
{
if (Connection.State == ConnectionState.Open)
{
Console.WriteLine("Its open..>!!!!");
}
Connection.Open();
MySqlCommand cmd = new MySqlCommand(query, Connection);
cmd.ExecuteNonQuery();
MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT LAST_INSERT_ID() AS 'identity'", Connection);
DataTable dt = new DataTable();
adapter.Fill(dt);
uint ret = 0;
if (dt.Rows.Count > 0)
{
try
{
ret = Convert.ToUInt32(dt.Rows[0]["identity"]);
}
catch
{
ret = 0;
}
}
else
ret = 0;
Connection.Close();
return ret;
}
}
我的连接字符串是
"server = 127.0.0.1; user id = ****; password = ****; database = testdb; pooling=true; maximumpoolsize=500;"
测试的线程是
static void DoInserts(object o)
{
int i = (int)o;
Console.WriteLine("Insert Thread {0} launched", i);
for(int x = 0; x < 1000; x++)
{
uint insertId = DataLayer.uNonQuery(String.Format("INSERT INTO testdb.dbtest (writeNo,strNo) VALUES ({0},'x={0} thread={1}')", x, i));
}
Console.WriteLine("Insert Thread {0} completed", i);
}
我得到异常Exception: Connection must be valid and open。在 2 个并发线程上进行大约 6 次插入后,我还需要做些什么才能使池正常工作吗?
谢谢