MySQL .Net connection pool connection.Open() very slow

最后都变了- 提交于 2019-12-11 13:59:07

问题


Version 6.4.4:

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?

Connection String:

<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;" />

Code

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();
            }

        }
    }
}

回答1:


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 :)




回答2:


The issue was also present on our side with MySql Connector .Net 6.6.5. The MySql Connector .Net 6.8.3 is solving this issue.

Fabrice



来源:https://stackoverflow.com/questions/8662409/mysql-net-connection-pool-connection-open-very-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!