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
1,118 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 11

Expert Comment

by:azarc3
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:azarc3
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
Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

 

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:azarc3
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

726 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