简体   繁体   English

DataAdapter不会自行关闭连接

[英]DataAdapter is not closing connection by itself

As we know that DataAdapter opens and closes a Connection if it is not already open. 众所周知,DataAdapter将打开并关闭Connection(如果尚未打开)。 But with my code it opens but not close..I am using MySql.Data.MySqlClient.MySqlDataAdapter, not sure what I am doing wrong. 但是随着我的代码,它打开但没有关闭..我正在使用MySql.Data.MySqlClient.MySqlDataAdapter,不确定我在做什么错。 below is my code 下面是我的代码

Public Function GetDT(ByVal SqlQuery As String, ByVal ConString As String) As DataTable
    Dim da As New MySql.Data.MySqlClient.MySqlDataAdapter(SqlQuery, ConString)
    Dim ds As New DataSet
    da.Fill(ds)
    GetDT = ds.Tables(0)
    da.Dispose()
    ds.Dispose()
    da = Nothing
    ds = Nothing
End Function

I am using this connection string: "server=localhost;port=3306;user=someuser;pwd=somepassword;database=mydatabasename;Allow Zero Datetime=True;" 我正在使用以下连接字符串: "server=localhost;port=3306;user=someuser;pwd=somepassword;database=mydatabasename;Allow Zero Datetime=True;"

while debugging the code I found as soon as DataAdapter.fill executes its start a connection thread, but no where connection is getting close as thread remains in SLEEP state . 在调试代码后,我发现DataAdapter.fill执行启动连接线程后立即发现该代码,但是当线程保持SLEEP状态时,连接不会关闭。 Please check the below image. 请检查下图。

在此处输入图片说明

Can any one please help me on this ? 有人可以帮我吗?

Thanks 谢谢

Its normal behavior because ADO.NET uses Connection Pooling with SQL Server by default. 它的正常行为是因为ADO.NET默认情况下将连接池与SQL Server一起使用。 From MSDN: 从MSDN:

http://msdn.microsoft.com/en-US/library/8xx3tyca(v=vs.110).aspx http://msdn.microsoft.com/en-US/library/8xx3tyca(v=vs.110).aspx

Connecting to a database server typically consists of several time-consuming steps. 连接到数据库服务器通常包括几个耗时的步骤。 A physical channel such as a socket or a named pipe must be established, the initial handshake with the server must occur, the connection string information must be parsed, the connection must be authenticated by the server, checks must be run for enlisting in the current transaction, and so on. 必须建立一个物理通道(例如套接字或命名管道),必须与服务器进行初始握手,必须解析连接字符串信息,必须由服务器对连接进行身份验证,必须运行检查以在当前环境中进行登记交易等等。

In practice, most applications use only one or a few different configurations for connections. 实际上,大多数应用程序仅使用一种或几种不同的配置进行连接。 This means that during application execution, many identical connections will be repeatedly opened and closed. 这意味着在应用程序执行期间,许多相同的连接将被反复打开和关闭。 To minimize the cost of opening connections, ADO.NET uses an optimization technique called connection pooling. 为了最大程度地减少打开连接的成本,ADO.NET使用了一种称为连接池的优化技术。

Connection pooling reduces the number of times that new connections must be opened. 连接池减少了必须打开新连接的次数。 The pooler maintains ownership of the physical connection. 池管理者维护物理连接的所有权。 It manages connections by keeping alive a set of active connections for each given connection configuration. 它通过为每个给定的连接配置保留一组活动的连接来管理连接。 Whenever a user calls Open on a connection, the pooler looks for an available connection in the pool. 每当用户在连接上调用“打开”时,池管理器就会在池中寻找可用的连接。 If a pooled connection is available, it returns it to the caller instead of opening a new connection. 如果池化连接可用,它将把它返回给调用者,而不是打开一个新连接。 When the application calls Close on the connection, the pooler returns it to the pooled set of active connections instead of closing it . 当应用程序在连接上调用Close时,池化程序将其返回到活动连接的池化集中,而不是将其关闭 Once the connection is returned to the pool, it is ready to be reused on the next Open call. 一旦将连接返回到池,就可以在下一个Open调用中重用该连接。

.... ....

The connection pooler removes a connection from the pool after it has been idle for approximately 4-8 minutes,... 连接池闲置了大约4-8分钟后,连接池将从池中删除连接,...

The effect of "using" or "dispose" in the DataAdapter is the same. 在DataAdapter中“使用”或“处置”的效果是相同的。 "using" ensures that dispose is executed, even if an exception happends. 即使发生异常,“使用”也可以确保执行处置。 Is the same as using a try / finally statment and put the "dispose" on the finally section. 与使用try / finally语句并将“ dispose”放置在finally节中相同。

The connection only closes itself when you use a Using directive, the Using statement can be used on any object that implements the IDisposable interface. 仅当您使用Using指令时,连接才会关闭, Using语句可用于实现IDisposable接口的任何对象。

In the Dispose() method of the DataAdapter the connection is closed before disposing the object. 在DataAdapter的Dispose()方法中,在处置对象之前关闭连接。 using it correctly would be something like this. 正确使用它将是这样的。

Public Function GetDT(ByVal SqlQuery As String, ByVal ConString As String) As DataTable
    Using da As New MySql.Data.MySqlClient.MySqlDataAdapter(SqlQuery, ConString)
        Dim ds As New DataSet
        da.Fill(ds)
        GetDT = ds.Tables(0)
        da.Dispose()   //because you are now using a 'Using' statement, this method is not necessary anymore.
        ds.Dispose()
        da = Nothing
        ds = Nothing
    End Using
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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