C# mysqlconnector - how to check for loss of con?

Red Squirrel

No Lifer
May 24, 2003
67,915
12,379
126
www.anyf.ca
What is a proper way to check at query time if the connection has been lost? This is the code where I'd like to have this, I just can't figure out how to do it. I do get a query error but that's too general, I can't assume that a query error is loss of connectivity. (in my case it most likely will be though). I *could* do a string search but that seems dirty as the error messages may vary.


public bool Query(string query)
{
/* if(m_con.State != m_con.ConnectionState.Open)
{
ThrowError("query...","Seem to have lost connection, will try to reconnect...");

try
{
m_con.Open();
}
catch
{
ThrowError(query,"FATAL:Unable to connect to DB");
return false;
}
}*/

try
{
if(m_Reader!=null) m_Reader.Close();

MySqlCommand command = m_con.CreateCommand();
command.CommandTimeout = 0;
command.CommandText = query;

m_Reader = command.ExecuteReader();
}
catch (Exception ex)
{
string exstr=ex.ToString()

/*
if( Connection is lost )
{
//handle properly
//set flag to mark this connection as lost so it can try to reconnect later
return false;
}*/


ThrowError(query,exstr);
return false;
}

Debugger.Write(String.Format("mysql{0}",m_conid),"Query:{0}",query);

return true;
}
 

nickbits

Diamond Member
Mar 10, 2008
4,122
1
81
You shouldn't need to hold onto the connection. .NET maintains a pool of connections itself. But aside from that just catch the exception and check the SqlException.Number property.

Disclaimer: I have only used MS SQL server but I am assuming the MySQL classes work in the same manner.
 

Red Squirrel

No Lifer
May 24, 2003
67,915
12,379
126
www.anyf.ca
m_con is of type MysqlConnection.

I think I figured it out, this seems to work so far:

public bool Query(string query)
{
//check if last query had a connection error
if(m_lostcon && !Reconnect())
{
Debugger.Write("mysqlcon","{0}: Query failed, no connection",m_conid);
return false;
}

//do query:
try
{
if(m_Reader!=null) m_Reader.Close();

MySqlCommand command = m_con.CreateCommand();
command.CommandTimeout = 0;
command.CommandText = query;

m_Reader = command.ExecuteReader();
}
catch (Exception ex)
{
string exstr=ex.ToString();

ThrowError(query,exstr);

//check if we lost con to server
if(m_con.State.ToString()!="Open")
{
Debugger.Write("mysqlcon","{0}: Lost connectivity to server.",m_conid);
m_lostcon=true;

//try to reconnect at least once:
if(Reconnect())
{
Debugger.Write("mysqlcon","{0}: Reconnected, retrying query...",m_conid);
return Query(query);
}

return false;
}
//----

return false;
}

Debugger.Write("mysql","Query:{0}",query);

return true;
}


There's also a state variable but not sure how consistant it is set so decided to check at the query instead to be extra safe. In my program I need to handle the query error then write the data in a cache until server is back up so it's important to catch it at the query.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Holding onto a connection object is generally a bad idea. When you're ready to interact with the database, establish the connection (ADO.NET will issue one from its pool), execute the query(ies) and close the connection (returning it to the pool). You'll know if you can't establish a connection right away.
 

Red Squirrel

No Lifer
May 24, 2003
67,915
12,379
126
www.anyf.ca
Originally posted by: MrChad
Holding onto a connection object is generally a bad idea. When you're ready to interact with the database, establish the connection (ADO.NET will issue one from its pool), execute the query(ies) and close the connection (returning it to the pool). You'll know if you can't establish a connection right away.


Well it depends. This app is constantly querying so it would make no sense to keep opening/closing the connection. Or would it not be an issue? I'm talking like, multiple queries per second.
 

imported_Dhaval00

Senior member
Jul 23, 2004
573
0
0
Even if you're issuing multiple commands, you should [in most logical implementations] close the connection. You have to remember that when you call Open() and Close(), you're not physically opening and closing a connection - you're simply accessing a connection from the pooler.

In short, let the pooler do its job and always open and close your connections. I have yet to see an ADO.NET application (ignoring the buggy ones) that persisted an open connection because of "performance issues."
 

Red Squirrel

No Lifer
May 24, 2003
67,915
12,379
126
www.anyf.ca
Originally posted by: Dhaval00
Even if you're issuing multiple commands, you should [in most logical implementations] close the connection. You have to remember that when you call Open() and Close(), you're not physically opening and closing a connection - you're simply accessing a connection from the pooler.

In short, let the pooler do its job and always open and close your connections. I have yet to see an ADO.NET application (ignoring the buggy ones) that persisted an open connection because of "performance issues."

Oh I see so the TCP connection still stays up? That may not be as bad then, I may go ahead and open/close. Though since I got it working without needing that, is there still an advantage to change it? I might just go ahead and do it anyway but I just want to get an idea of why it's better.

Also what does ADO.net refer to, is that something just for SQL or will it work the same way in MySQL?
 

JasonCoder

Golden Member
Feb 23, 2005
1,893
1
81
If you're issuing a ton of queries in a logical unit of work than I would go ahead and keep the connection open. Probably not terrible to open/close after each operation but if you're doing, say, 100 queries in a loop, I'd keep the connection around.

ADO.Net is the collective name of data access libraries in .Net. Your connection and command objects you are using are ADO.Net objects.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |