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,111 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
Industry Leaders: 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: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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Select question from MySQL 1 20
PHP: Filling Out/Creating a PDF 29 102
how to make geography query faster?  SQL 7 45
T-SQL: How to extract records into a new table 7 19
Creating and Managing Databases with phpMyAdmin in cPanel.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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