连接丢失后自动重新连接到MySql

发布于 2024-08-05 00:40:32 字数 119 浏览 4 评论 0原文

我有一个使用 MySql 数据库的 c# 应用程序,问题是在一段时间不活动(8 小时)后或与数据库所在的服务器的连接丢失时,与数据库的连接将关闭并且无法执行数据库查询。我如何启用自动重新连接到数据库。

此致。

I have a c# application that uses a MySql database, the problem that after a period of inactivity (8 hours) or when connection to the server where database is host is lost, the connection to the database is closed and database queries cannot be executed. how can i enable the auto reconnect to the database.

Best regards.

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

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

发布评论

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

评论(5

暮凉 2024-08-12 00:40:32

我认为首先应该解决的问题是,为什么连接会连续开放 8 个小时?

我认为在大多数情况下,您将连接到数据库,执行您的操作,然后关闭它:

using (var conn = new MySqlConnection(connString))
{
  // do what you need here
}

using 块将创建一个仅在该块内有效的连接。当块结束时,将在连接上调用 Dispose 方法并关闭并正确处置连接对象。这将防止您的应用程序在不需要时耗尽可用连接。

如果您需要确定连接是否打开,您的连接变量将具有一个State属性,该属性将是一个ConnectionState枚举。

您可以使用以下内容进行检查:

if (conn.State != ConnectionState.Open)
{
  conn = new MySqlConnection(connString);
}

希望有帮助。

I think the first thing that should be addressed is, why would a connection be open for 8 hours straight?

I'd think that in most cases you'll connect to the database, do your thing, and close it:

using (var conn = new MySqlConnection(connString))
{
  // do what you need here
}

The using block will create a connection which is only valid within the block. When the block ends, the Dispose method will be called on the connection and close and properly dispose of the connection object. This will prevent your application from using up available connections when they aren't needed.

If you are in a situation where you'll need to determine if the connection is open, your connection variable will have a State property which will be a ConnectionState enumeration.

You can check it using something like this:

if (conn.State != ConnectionState.Open)
{
  conn = new MySqlConnection(connString);
}

Hope that helps.

白云悠悠 2024-08-12 00:40:32

在您的应用程序中,在进行任何查询之前,请测试您的连接是否仍处于活动状态。如果不是,则重新连接。等等瞧!

In your application, before any queries, test if your connection is still active. If it isn't, then reconnect it. Et voila !

千と千尋 2024-08-12 00:40:32

如果尝试访问数据库失败,请再次运行连接命令。

Run your connection command again if the attempt to access the database fails.

转角预定愛 2024-08-12 00:40:32

您可以在连接字符串中使用选项 keepalivekeepalivetimeout。最好使用 MySqlConnectionStringBuilder 来实现这一点。

You can use the option keepalive and keepalivetimeout in the connection string. It is best to use the MySqlConnectionStringBuilder for that.

意中人 2024-08-12 00:40:32

感谢您的回答和建议。我有此要求是因为持久性被委托给非托管代码,并且在我的 C# 代码中我只调用此 API。

我通过更改 wait_timeout MySQL 参数解决了我的问题,该参数将 28800 秒(8 小时)作为不活动时间的默认值。

Thank you for your answers and suggestions. I have this requirement because persistence is delegated to unmanaged code and in my C# code I only make calls to this API.

I resolved my problem by changing the wait_timeout MySQL parameter that has 28800 seconds (8 hours) as the default value for the period of inactivity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文