Connection state MySql Connector/NET
I'm using the latest stable MySql Connector/NET 6.5.4.0.
I open a connection to a MySQL database. In the C# code, the Connection.State
property is Open
. I do some magic stuf, and while I'm doing that, I kill the connection server side. However, in the code the State is still Open
.
I ran into this problem because I save instances of my database class in a static variable per session (Dictionary). If a user does a request, the database class is pulled from this variable, and queries are fired to it. However, if the connection closes server side (killed by de sysadmin, wait timeout elapsed), the state isn't updated.
Is there a workaround for this problem? My colleague allready submitted a bug report for it (http://bugs.mysql.com/bug.php?id=64991).
Close and Open before execution, is very bad for the performance, so no option.
Putting aside design issues (should really be pooling), based on your comment:
Connection pooling is turned off in this case, I'll look into the Ping method. If this connection is closed, I get an 'Fatal error encountered during command execution'
Is there any reason you're not recreating the connection if the ping fails?
private static MySqlConnection conn;
private static int maxRetries;
private static void connect()
{
conn = <connect code>;
}
private static MySqlConection GetConnectionRetry(int count)
{
if (conn != null && conn.State == Open)
{
try
{
conn.Ping();
return conn;
}
catch (Exception e) // better be specific here
{
if (count <= maxRetries)
{
connect();
return GetConnectionRetry(count + 1);
}
else throw e;
}
}
else
{
connect();
return GetConnectionRetry(count + 1);
}
}
static MySqlConnection GetConnection()
{
return GetConnectionRetry(1);
}
I would advise you to use the singelton-pattern for the connection. So you can ensure that there is always just one connection open and you only have to manage this one.
链接地址: http://www.djcxy.com/p/60974.html下一篇: 连接状态MySql连接器/ NET