如果与 MySQL 服务器的连接丢失,是否可以创建事件并处理它?

发布于 2024-07-22 06:38:56 字数 479 浏览 2 评论 0原文

假设我有客户端应用程序,它连接到 MySQL 服务器。 极好的。 我不将其实现为线程。 我只使用 MySQLConnection 类。

现在,假设吉姆对办公室非常粗心,在未经许可的情况下意外地重新启动了 MySQL 服务器。

我想知道断开连接何时发生,以便客户端应用程序知道并且用户不会收到很多错误。

我可以想出几种方法来检查这一点,但我不知道它们是否有效,即使它们设计得很优雅。 我想知道你们认为最合适的方式是什么。

  • 将连接实现为一个线程,并每 x 秒检查一次(如果可能的话)
  • 在连接断开时创建并处理一个事件(有点依赖于上面的一点)
  • 也许我在 MySQL 连接器库中内置了一个事件甚至不知道
  • 我不知道的其他一些神秘的黑暗代码

非常感谢任何帮助。

编辑:这个问题已在评论中得到回答。 谢谢阅读。

Let's say I have my client application and it makes a connection to the MySQL server. Fantastic. I don't implement this as a thread. I just utilise the MySQLConnection class.

And now let's say Jim who's really careless about the office accidently reboots the MySQL server without asking permission first.

I want to know when the disconnection happens so the client application is aware and the user doesn't get a lot of errors.

I can think of a few ways to check this but I don't know if they'll work and even if they're elegant in design. I was wondering what you folks thought was the most appropriate way.

  • Implement the connection as a thread and check it every x seconds (if it's even possible)
  • Create and handle an event when the connection drops (sort of relies on the point above)
  • Maybe there's a event built into the MySQL Connector library that I'm not even aware of
  • Some other mystical dark code that I'm not aware of

Any help is very much appreciated.

Edit: This question has been answered in comments. Thanks for reading.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别挽留 2024-07-29 06:38:56

我有一个“偶尔连接”的应用程序并遇到了这个问题。 我需要知道用户是否有网络连接,如果有,他们是否有允许他们连接到 SQL Server 数据库的 VPN 连接。 我使用计时器每分钟对数据库服务器执行一次 ping 操作,以检查其可达性。

    public NetworkStatus GetNetworkStatus()
    {
        NetworkStatus netStatus = new NetworkStatus();
        netStatus.NetworkAvailable = NetworkInterface.GetIsNetworkAvailable();

        try
        {
            if (netStatus.NetworkAvailable)
            {
                Ping p = new Ping();
                // Create a buffer of 32 bytes of data to be transmitted.
                string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
                byte[] buffer = Encoding.ASCII.GetBytes(data);
                PingReply reply = p.Send(DB_SERVER_NAME, 1200, buffer);
                netStatus.PublisherPingSuccess = reply.Status == IPStatus.Success;
            }
        }
        catch (Exception ex)
        {
            netStatus.PublisherPingSuccess = false;
            netStatus.Error = ex;
        } 
        return netStatus;
    }

I have an "occasionally connected" application and faced this problem. I needed to know if the user had a network connection and, if they did, if they had a VPN connection that would allow them to connect to the SQL Server database. I used a timer to ping the database server once a minute to check its reachability.

    public NetworkStatus GetNetworkStatus()
    {
        NetworkStatus netStatus = new NetworkStatus();
        netStatus.NetworkAvailable = NetworkInterface.GetIsNetworkAvailable();

        try
        {
            if (netStatus.NetworkAvailable)
            {
                Ping p = new Ping();
                // Create a buffer of 32 bytes of data to be transmitted.
                string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
                byte[] buffer = Encoding.ASCII.GetBytes(data);
                PingReply reply = p.Send(DB_SERVER_NAME, 1200, buffer);
                netStatus.PublisherPingSuccess = reply.Status == IPStatus.Success;
            }
        }
        catch (Exception ex)
        {
            netStatus.PublisherPingSuccess = false;
            netStatus.Error = ex;
        } 
        return netStatus;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文