MySQL .Net连接池connection.Open()非常慢 [英] MySQL .Net connection pool connection.Open() very slow

查看:545
本文介绍了MySQL .Net连接池connection.Open()非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

版本6.4.4:

使用MySqlConnection的最基本实现,以下代码在预加载连接池以达到我的连接字符串中配置的最小池大小"时,每个连接需要2-5秒.

Using the most basic implementation of MySqlConnection, the following code takes 2-5 seconds per connection when preloading the connection pool to reach the "Min Pool Size" configured in my connection string.

有什么想法为什么要花这么长时间,如何修复或解决方法?

Any ideas why it is taking so long, how to fix, or workarounds?

连接字符串:

<add name="users" connectionString="server=192.168.1.2;User Id=dbuser;password=dbpassword;database=users;Pooling=true;Min Pool Size=10;Max Pool Size=50;Persist Security Info=True;" />

代码

private static void MySqlConnectionTester()
{
    string connectionString = ConfigurationManager.ConnectionStrings["users"].ConnectionString;

    using (var connection = new MySqlConnection(connectionString))
    {
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "select * from users;";

            try
            {
                connection.Open(); // This line hangs until "Min Pool Size" is reached.
                using (var reader = command.ExecuteReader())
                {
                    while(reader.Read())
                    {
                        // Read results
                    }
                }
            }
            catch(Exception ex)
            {
                // Log exception
            }
            finally
            {
                connection.Close();
            }

        }
    }
}

推荐答案

At MySQL's homepage they write that you should avoid creating, opening and closing the connection object youself, instead you should use the helper class which should work better with connectionpooling.

我还没有测试过,但是我刚刚读过它:)

I have not tested it, but was something I just read :)

这篇关于MySQL .Net连接池connection.Open()非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆