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,097 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: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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now