2

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");
        }
2
  • Is this an ASP.NET application? Do you have connection caching enabled? Commented Jun 26, 2014 at 17:45
  • Sounds like connection pooling in action. Commented Jun 26, 2014 at 17:45

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.