如何查询MySQL的.Net连接器的连接池的当前大小?

是否有一种编程方式来查找由.Net connector / MySql.Data.dll维护的数据库的当前打开连接数?

我有兴趣在使用库的同一程序中收集此信息。

连接池在客户端执行。 要访问它,您需要使用reflection来访问MySqlPoolManager和MySqlPool类,它们都是MySql.Data程序集的内部类。

从本质上讲,您将需要使用reflection来到池中。 这是如何做:

Assembly ms = Assembly.LoadFrom("MySql.Data.dll"); Type type = ms.GetType("MySql.Data.MySqlClient.MySqlPoolManager"); MethodInfo mi = type.GetMethod("GetPool", BindingFlags.Static | BindingFlags.Public); var pool = mi.Invoke(null, new object[] { new MySqlConnectionStringBuilder(connString) }); 

您会注意到您必须传入MySqlConnectionStringBuilder对象。 它为每个连接字符串创建一个单独的池,因此使用您在应用程序中使用的相同连接字符串(它必须完全相同)。

然后,您可以访问私有池字段和属性(再次使用reflection),以获取所需的信息。 特别是,“可用”字段和“NumConnections”属性可能是您感兴趣的。 还有“idlePool”( Queue<> )和“inUsePool”( List<> ),您也可以访问它们,特别是计数。

感谢Petes回答这个问题和另一个答案,我能够建立以下function:

  /// totalAvailable, inUseCount, idleCount public int[] GetPoolStatsViaReflection(MySqlConnectionStringBuilder connectionStringBuilder) { var asm = typeof(MySqlConnectionStringBuilder).Assembly; var poolManagerType = asm.GetType("MySql.Data.MySqlClient.MySqlPoolManager"); // internal sealed var poolType = asm.GetType("MySql.Data.MySqlClient.MySqlPool"); // internal sealed var pool = poolManagerType.InvokeMember("GetPool", System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public, null, null, new object[] { connectionStringBuilder }); const System.Reflection.BindingFlags nonPublicInstanceField = System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance; var totalAvailable = (int)poolType.InvokeMember("available", nonPublicInstanceField, null, pool, new object[] { }); // List var inUsePool = (System.Collections.ICollection)poolType.InvokeMember("inUsePool", nonPublicInstanceField, null, pool, new object[] { }); // Queue var idlePool = (System.Collections.ICollection)poolType.InvokeMember("idlePool", nonPublicInstanceField, null, pool, new object[] { }); return new[] {totalAvailable, inUsePool.Count, idlePool.Count}; }