简体   繁体   中英

MySql Connection not closing using .net MySql Connector

MySql Connection is going to sleep mode instead of close in mysql. I am using MySql.Data 6.5.4 version to communicate with mysql. I am not sure what am I doing wrong in below code.

   try
        {
            using (var conn = new MySqlConnection(strConnection))
            {
                conn.Open();

                using (var cmd = new MySqlCommand(strSQLStatement, conn))
                {
                    var adapter = new MySqlDataAdapter(cmd);
                    adapter.Fill(ds);
                }
            }
        }
        catch (Exception ex)
        {
            ErrorLogger.WriteError(string.Format("Failed query {0} with stack trace {1} ", strSQLStatement, ex), "GetData");
        }

Your connection is being added to the connection pool, this feature is on by default so whenever a connection is closed it is added to a connection pool. You can either solve this problem by connection string parameter Pooling=false or the static methods MySqlConnection.ClearPool(connection) and MySqlConnection.ClearAllPools() ...

From Official Documentation

The Connector/Net supports connection pooling for better performance and scalability with database-intensive applications. This is enabled by default. You can turn it off or adjust its performance characteristics using the connection string options Pooling, Connection Reset, Connection Lifetime, Cache Server Properties, Max Pool Size and Min Pool Size.

Connection pooling works by keeping the native connection to the server live when the client disposes of a MySqlConnection. Subsequently, if a new MySqlConnection object is opened, it will be created from the connection pool, rather than creating a new native connection. This improves performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM