Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MySQL connection doesn't close or dispose in Workbench when it's suppose to close in my vb.net program

Posted on 2010-11-09
6
Medium Priority
?
1,147 Views
Last Modified: 2012-05-10
My VB.Net / Windows program starts with a log in screen. When the form loads, it gets all the user names and sets it as AutoCompleteStringCollection. The sub procedure is below:

If I go through the code line by line while looking at the MySQL WorkBench, the line:

 myConnection.Open()"

opens a connection but the lines

MyConnection.close or MyConnection.dispose doesn't close off the connection.

I use the same pattern in other queries without any problems.
Private Sub GetListOfUserNames()

Try
 Dim myConnection As MySqlConnection
 myConnection = New MySqlConnection(strConnection)
 myConnection.Open()
 Dim daUser As New MySqlDataAdapter("Select strUsername from Users order by strUsername asc", myConnection)
 Dim dsUser As New DataSet
 daUser.Fill(dsUser)
 myConnection.Dispose()
 For i As Integer = 0 To dsUser.Tables(0).Rows.Count - 1
  strNames.Add(dsUser.Tables(0).Rows(i)("strUsername").ToString)
 Next
    Catch ex As Exception
       MsgBox("Error getting Username Information.")
    End Try
End Sub

Open in new window

0
Comment
Question by:JohnnyBCJ
  • 3
  • 3
6 Comments
 
LVL 11

Expert Comment

by:Kelvin McDaniel
ID: 34096376
If I'm not mistaken it's staying open because daUser is still active with references. Suggest you try the following pattern instead as it's guaranteed to close the resources enclosed in the "Using" block... and please note this requires .NET Framework v2.0 or later.


Private Sub GetListOfUserNames()
	Try
		Using myConnection As New MySqlConnection(strConnection)
			myConnection.Open()
			Dim dsUser As New System.Data.DataSet

			Using daUser As New MySqlDataAdapter("Select strUsername from Users order by strUsername asc", myConnection)
				daUser.Fill(dsUser)
				For i As Integer = 0 To dsUser.Tables(0).Rows.Count - 1
					strNames.Add(dsUser.Tables(0).Rows(i)("strUsername").ToString)
				Next
			End Using
		End Using
	Catch ex As Exception
		MsgBox("Error getting Username Information.")
	End Try
End Sub

Open in new window

0
 

Accepted Solution

by:
JohnnyBCJ earned 0 total points
ID: 34120909
I attempted what you suggested but it never solved my problem.

What solved my problem was adding Pooling=False; to the connection string.

I'm not sure how much of a performance hit this is going to cause but I believe this is the best route at the moment.
0
 
LVL 11

Expert Comment

by:Kelvin McDaniel
ID: 34136214
JohnnyBCJ,

I don't have a problem with you closing the question and not awarding points, but please be advised that unless there's a bug in the driver your solution hasn't solved the problem. In the connection string line, Connection Pooling instructs the connection manager to only request a connection if there's an open slot in the pool. Therefore, your setting simply tells it to always forcibly request a connection.

You may want to repost this question with the tag ADO.NET to see if those experts can provide deeper assistance.

Good Luck!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:JohnnyBCJ
ID: 34136364
If connection pooling only allows a connection to go through if there is an open slot in the pool, why does setting it to false close the connection when it hits the connection.dispose line? If connection pooling only has to do with opening up a connection, why does it make a difference when closing one?

If there is an error or bug in the driver, how could I detect it?

I added the ADO.Net tag as you suggested to see if other experts could give me a clearer image of what is happening here. I never re-posted the question as I think this situation fits under the 'if it isn't broken, don't fix it' category.
0
 
LVL 11

Expert Comment

by:Kelvin McDaniel
ID: 34137999
Reposting was only a suggestion. Definitely your call there as it's definitely your points at stake.  :)

I suggested that this may be a bug (and therefore an improperly implemented provider/driver) because I don't see in your code where any "other" references to your objects would cause the close/dispose processes to malfunction/not run as aspected.  Setting connection pooling to "false" should not control this.

I'm also a firm believer in "Get it done", and in all honesty you would probably have to write a wrapper class for the MySqlConnection object to get it to function "right".
0
 

Author Closing Comment

by:JohnnyBCJ
ID: 34153301
My comment is how I solved my problem.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question