[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
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
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:
If conn.ConnectionState=Conn.Closed Then
conn.open()
End If
Alternately, you can close the connection after each query is done, and reopen it when you need to process a query.
Re: MYsql - how to reconnect if database connection timed out?
Quote:
Originally Posted by
shiznit770
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!