连接状态MySql Connector / NET [英] Connection state MySql Connector/NET

查看:165
本文介绍了连接状态MySql Connector / NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是最新的稳定的MySql Connector / NET 6.5.4.0。

I'm using the latest stable MySql Connector/NET 6.5.4.0.

我打开一个连接到MySQL数据库。在C#代码中, Connection.State 属性是打开
我做一些魔术stuf,虽然我这样做,我杀了连接服务器端。但是,在代码中,状态仍然是打开

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.

我碰到这个问题,数据库类在每个会话的静态变量(字典)。
如果用户执行请求,则从此变量中提取数据库类,并向其发出查询。
然而,如果连接关闭服务器端(由de sysadmin杀死,等待超时时间已过),状态不会更新。

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.

这个问题?我的同事已经提交了一个错误报告(http://bugs.mysql.com/bug.php?id=64991)。

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:


在这种情况下,连接池已关闭,我将研究Ping
方法。如果这个连接关闭,我得到一个'命令执行过程中遇到的致命错误

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'

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);
}

这篇关于连接状态MySql Connector / NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆