Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

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,138 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: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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
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

618 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