在C#中使用的MySqlConnection不关闭正确 [英] Using MySQLConnection in C# does not close properly

查看:3355
本文介绍了在C#中使用的MySqlConnection不关闭正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

连接被添加到连接池。所以我关闭了它,但它仍保持身体打开。同的ConnectionString参数池=假或静态方法MySqlConnection.ClearPool(连接)和MySqlConnection.ClearAllPools可避免的问题。需要注意的是,问题是,该连接还活着,当我关闭应用程序。即使我关闭了它。因此,无论我根本不使用连接池或我清除特定池关闭连接之前,问题就解决了​​。我把我的时间搞清楚什么是在我的情况下,最好的解决方案。

The connection was added to the connection pool. So I closed it, but it still remained physically open. With the ConnectionString Parameter "Pooling=false" or the static methods MySqlConnection.ClearPool(connection) and MySqlConnection.ClearAllPools the problem can be avoided. Note that the problem was, that the connection was still alive when I closed the application. Even though I closed it. So either I don't use connection pooling at all or I clear the specific pool before closing the connection and the problem is solved. I'll take my time figuring out what's the best solution in my case.

感谢所有谁回答!它帮助我了解C#更好的概念和我从有益的投入了很多的经验教训。 :)

Thanks to all who answered! It helped my understand the concepts of C# better and I learned alot from the useful input. :)

===

我搜索过,而现在并没有发现我的问题的解决方案:
我是新的C#和尝试写一个类来使MySQL连接更加容易。我的问题是,我打开连接并关闭它后。它仍然是在数据库打开和被中止。

I've searched for a while now and haven't found the solution for my problem: I'm new to C# and try to write a class to make MySql Connections easier. My problem is, after I open a connection and close it. It is still open in the Database and gets aborted.

我使用过程中的'使用'声明',但连接仍然是开放后,我被中止退出程序

I'm using the 'using' statement' of course, but the connection is still open and gets aborted after I exit the program.

下面就是我的代码如下:

Here's what my code looks like:

using (DatabaseManager db = new DatabaseManager())
{
using (MySqlDataReader result = db.DataReader("SELECT * FROM module WHERE Active=1 ORDER BY Sequence ASC"))
{
    foreach (MySqlDataReader result in db.DataReader("SELECT * FROM module WHERE Active=1 ORDER BY Sequence ASC"))
    {
        //Do stuff here
    }
}
}

类数据库管理器打开连接和处置时关闭它:

The class Database manager opens the connection and closes it when disposed:

public DatabaseManager()
{
    this.connectionString = new MySqlConnectionStringBuilder("Server=localhost;Database=businessplan;Uid=root;");
    connect();
}
private bool connect()
{
    bool returnValue = true;
    connection = new MySqlConnection(connectionString.GetConnectionString(false));
    connection.Open();
}

public void Dispose()
{
    Dispose(true);
}

public void Dispose(bool disposing)
{
    if (disposing)
    {
        if (connection.State == System.Data.ConnectionState.Open)
        {
            connection.Close();
            connection.Dispose();
        }
    }
    //GC.SuppressFinalize(this);//Updated
}
//Updated
//~DatabaseManager()
//{
//  Dispose(false);
//}



所以,我检查在调试器和Dispose()方法 - 方法被调用,并正确执行。
我在想什么?是不是我做错了或误解?

So, I checked it in the debugger and the Dispose()-method is called and executes correctly. What am I missing? Is there something I did wrong or misunderstood?

任何帮助表示赞赏!

问候,
西蒙

PS:
以防万一,DataReader的() - 方法(更新版):

P.S.: Just in case, the DataReader()-method (Updated version):

public IEnumerable<IDataReader> DataReader(String query)
    {
        using (MySqlCommand com = new MySqlCommand())
        {
            com.Connection = connection;
            com.CommandText = query;
            using (MySqlDataReader result = com.ExecuteReader(System.Data.CommandBehavior.CloseConnection))
            {
                while (result.Read())
                {
                    yield return (IDataReader)result;
                }
            }
        }
    }






好吧,我试图用收益回报:


Ok, I tried to use the yield return:

foreach (MySqlDataReader result in db.DataReader("SELECT * FROM module WHERE Active=1 ORDER BY Sequence ASC"))
{
    //...
}

和我改变了的DataReader法:

And I changed the DataReader-method:

public IEnumerable<IDataReader> DataReader(String query)
    {
        using (MySqlCommand com = new MySqlCommand())
        {
            com.Connection = connection;
            com.CommandText = query;
            using (MySqlDataReader result = com.ExecuteReader())
            {
                while (result.Read())
                {
                    yield return (IDataReader)result;
                }
            }
        }
    }



据在工作​​,我可以检索数据的方式,但我仍然有同样的问题:连接不正确关闭。 (

It works in the way that I can retrieve the data, yet I still have the same problem: The connection isn't closed properly. :(

推荐答案

林不确定的MySqlConnection,但在SQL Server计数器部分使用连接池,当你调用close相反,它不会关闭!把它连接池中

Im unsure about mysqlconnection but the sql server counter part uses Connection pooling and does not close when you call close instead it puts it in the connection pool!

编辑:确保你处置器,命令和连接对象

Make sure you dispose the Reader, Command, and Connection object!

编辑:用ConnectionString的参数解决了池=假或静态方法MySqlConnection.ClearPool(连接)和MySqlConnection.ClearAllPools()

Solved with the ConnectionString Parameter "Pooling=false" or the static methods MySqlConnection.ClearPool(connection) and MySqlConnection.ClearAllPools()

这篇关于在C#中使用的MySqlConnection不关闭正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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