• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1145
  • Last Modified:

mysql connection not closing. Too many connections error

I have create a simple .NET windows application which connects to MySQL database to select and insert data. Please see attached code. Iam accessing about four tables in MySQL to load a grid. Upon loading the grid, the connections are not closing and their status is "Sleep" when I check "Show Processlist".

I am calling mysqlconn.close() after every function. Do I need to do anything else. The connections get closed only when the windows application is closed.

This applciation is used in such a way that users open it once and click the "process" button (Click event btnprocessrt_click) several times before closing the application. The event is creating 3-4 connections for every click and the Database is reaching the maximum connections. How can this be fixed?
  • 2
2 Solutions
RobOwner (Aidellio)Commented:
I've had some success by also setting the mysql connection object to nothing or null even though the garbage collector should be taking care of it
Julian HansenCommented:
Also I would use a different approach. Instead of creating a new connection each time you want to access the database. Open it once and reuse the same connection.

Keep it in a singleton class or similar where you can do a request for the connection which creates the connection first time and then returns that connection on each subsequent request.
Angel02Author Commented:
Well, I figured out the unbelievable mistake I made. I was calling Return before closing the connection. And I did that in more than one function..

If CInt(mySQLds.Tables(0).Rows(0).Item(0).ToString()) > 0 Then
                Return True
                Return False
            End If

When I fixed this, the number of connections stopped multiplying.

@ julianH your suggestion does make sense. I am going to use that approach. Thanks!
Angel02Author Commented:
Figured out what mistake I made in my code. Details are above.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now