Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8080055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:18:27+00:00 2026-06-05T16:18:27+00:00

I’m using Asp.Net together with MySql and I’m trying to solve the error: Timeout

  • 0

I’m using Asp.Net together with MySql and I’m trying to solve the error:

Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

I’m trying to make sure that I’m not leaking any connections.
I need to know the status of the connection pool (how many connections are open right now)? In this thread I learned that I can use the Performance Monitor to know the pool count. But how can I do that when using MySql instead of Sql Server?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:18:29+00:00Added an answer on June 5, 2026 at 4:18 pm

    Original Reference for the above code discussed below is here:

    I’ve done the following with Oracle, I think it can work with MySQL with some small modifications (e.g. change OracleConnection, etc.). You can create a class called ConnectionSpy (see below). This is in VB.Net, you could convert the code to C# if you choose.

    Imports System
    Imports System.Data
    Imports Oracle.DataAccess.Client
    Imports System.Diagnostics
    Imports System.IO
    
    Public Class ConnectionSpy
        Dim con As OracleConnection
        Dim st As StackTrace
        Dim handler As StateChangeEventHandler
        Dim logfileName As String
    
        Public Sub New(ByVal con As OracleConnection, ByVal st As StackTrace, ByVal logfileName As String)
            If (logfileName Is Nothing Or logfileName.Trim().Length() = 0) Then
                Throw New ArgumentException("The logfileName cannot be null or empty", logfileName)
            End If
    
            Me.logfileName = logfileName
            Me.st = st
            'latch on to the connection
            Me.con = con
            handler = AddressOf StateChange
            AddHandler con.StateChange, handler
            'substitute the spy's finalizer for the 
            'connection's
            GC.SuppressFinalize(con)
        End Sub
        Public Sub StateChange(ByVal sender As Object, ByVal args As System.Data.StateChangeEventArgs)
            If args.CurrentState = ConnectionState.Closed Then
                'detach the spy object and let it float away into space
                'if the connection and the spy are already in the FReachable queue
                'GC.SuppressFinalize doesn't do anyting.
                GC.SuppressFinalize(Me)
                RemoveHandler con.StateChange, handler
                con = Nothing
                st = Nothing
            End If
        End Sub
        Protected Overrides Sub Finalize()
            'if we got here then the connection was not closed.
            Using stream As FileStream = New FileStream( _
                        logfileName, _
                        FileMode.Append, _
                        FileAccess.Write, _
                        FileShare.Write)
    
                Using sw As StreamWriter = New StreamWriter(stream)
                    Dim text As String = _
                        DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & _
                        ": WARNING: Open Connection is being Garbage Collected" & _
                        Environment.NewLine & "The connection was initially opened " & st.ToString()
                    sw.WriteLine(text)
                    sw.Flush()
                    RemoveHandler con.StateChange, handler
                    'clean up the connection
                    con.Dispose()
                End Using
    
            End Using
        End Sub
    End Class
    

    Then, in your web.config/app.config, you can add a section like this:

    <configuration>
        <appSettings>
            <!--
              if you need to track connections that are not being closed, set
              UseConnectionSpy to "true". If you use the connection spy, a log
              will be created (the logfile can be specified with the ConnectionSpyLog
              parameter) which will give you a stack trace of each code location
              where a connection was not closed        
            -->
        <add key="UseConnectionSpy" value="true"/>
        <add key="ConnectionSpyLog" value="c:\\log\\connectionspy.log"/>
        </appSettings>
    </configuration>
    

    Then, wherever in your code you create a DB connection, you can do this (see below).The mConnection variable would represent your MySQL connection, which you would have already created when you get to this point.

        If ConfigurationManager.AppSettings("UseConnectionSpy") = "true" Then
            Dim st As StackTrace = New StackTrace(True)
            Dim sl As ConnectionSpy = New ConnectionSpy(mConnection, st, _
                                                        ConfigurationManager.AppSettings("ConnectionSpyLog"))
        End If
    

    So then, if you have any connections that are being garbage collected before they’re closed, it will be reported in the log file. I’ve used this technique in some of my applications and it works well.

    What you would need to do is create a wrapper class for your MySQL DB Connections, and you can put the above code in place to track the connections. Or, you could just add the above calls to wherever you create your connections. The wrapper class is a bit cleaner though (that’s what we did)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.