Results 1 to 4 of 4

Thread: [RESOLVED] MYsql - how to reconnect if database connection timed out?

Hybrid View

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    48

    Resolved [RESOLVED] MYsql - how to reconnect if database connection timed out?

    I have some software that grabs information from a mysql server. However, users minimize it to the system tray, and leave it idle for hours until using it again.

    If a user leaves the program idling for about 15 minutes, then goes into the program again to perform a query, it throws a Fatal Error in the mysql, and says 'connection must be valid and open'

    The reason is simply because the mysql connection has timed out..

    I was wondering what the code would be to re-connect to the server IF it has timed out.


    I tried simply putting


    try
    conn.Open()
    Catch
    End try


    so that it tries to connect, and if it can't connect (because the connection is already open), it just carries on, however it does not work, and it still gives an exception because it timed out.

    Any ideas?



    What would be the connection state once it has timed out? I tried putting it in a thing like


    if conn.connectionstate = connectionstate.Closed Or connectionstate.Broken

    etc, however it doesn't seem to be working.


    Any ideas?

    Thanks

  2. #2
    New Member
    Join Date
    May 2009
    Posts
    14

    Re: MYsql - how to reconnect if database connection timed out?

    Have you tried handling the connection object's StageChange event?

    Code:
        
    AddHandler conn.StateChange, AddressOf conn_Timeout
    
    Protected Sub conn_Timeout(ByVal sender As Object, ByVal e As System.Data.StateChangeEventArgs)
            If e.CurrentState = ConnectionState.Broken Or e.CurrentState = ConnectionState.Closed Then
                conn.Open()
            End If
        End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    48

    Re: MYsql - how to reconnect if database connection timed out?

    Quote Originally Posted by shiznit770 View Post
    Have you tried handling the connection object's StageChange event?

    Code:
        
    AddHandler conn.StateChange, AddressOf conn_Timeout
    
    Protected Sub conn_Timeout(ByVal sender As Object, ByVal e As System.Data.StateChangeEventArgs)
            If e.CurrentState = ConnectionState.Broken Or e.CurrentState = ConnectionState.Closed Then
                conn.Open()
            End If
        End Sub
    this will be perfect, thank you very much!

  4. #4

    Re: MYsql - how to reconnect if database connection timed out?

    Use a Timer that closes the connection after 5 minutes of idling.
    When the user does a query, do a check like so:
    vb Code:
    1. If conn.ConnectionState=Conn.Closed Then
    2. conn.open()
    3. End If

    Alternately, you can close the connection after each query is done, and reopen it when you need to process a query.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width