如果与 MySQL 服务器的连接丢失,是否可以创建事件并处理它?
假设我有客户端应用程序,它连接到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有一个“偶尔连接”的应用程序并遇到了这个问题。 我需要知道用户是否有网络连接,如果有,他们是否有允许他们连接到 SQL Server 数据库的 VPN 连接。 我使用计时器每分钟对数据库服务器执行一次 ping 操作,以检查其可达性。
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.