准备好的语句的数据库连接

发布于 2024-08-20 08:23:00 字数 262 浏览 3 评论 0原文

使用 ODBC 连接到 mysql 数据库时,连接会在 8 小时(默认值)后超时。

为了使我的应用程序具有弹性,它必须重新创建此连接以发出准备好的语句查询。

是否可以为准备好的语句发出新的数据库连接?

这样做有意义吗?

看起来准备好的语句是在连接的基础上创建的。

当前的解决方案是:

  1. 重新连接
  2. 重新准备查询
  3. 执行查询

Using an ODBC connection to a mysql database, the connection times-out after a period of 8 hours (the default).

In order for my application to be resilient, it must recreate this connection to issue a prepared statement query.

Is it possible to issue a new database connection for a prepared statement?

Does it make sense to do so?

It appears like prepared statements are created on a connection basis.

The current solution is to :

  1. Reconnect
  2. Re-prepare the queries
  3. Execute query

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

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

发布评论

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

评论(2

舟遥客 2024-08-27 08:23:00

不,准备好的语句与连接相关联,而不是相反。您当前处理超时连接的方式很好。

No, prepared statements are tied to a connection, not the other way around. Your current way of handling a timed out connection is fine.

清泪尽 2024-08-27 08:23:00

我认为,打开 SQL 数据库连接 8 小时是一种不好的做法。您是否有一些长时间运行的操作,需要很多时间?然后考虑在数据库中有一个存储过程,它将结果转储到某个临时表中,供您稍后检查。

但通常情况下,应用程序应该尽快释放连接(以及所有与之相关的准备好的语句)。为了加速连接打开,您可以使用连接池(ODBC 也应该存在)。连接池将在将连接返回给应用程序之前检查连接是否“活动”,例如,请参阅 Java DBCP

I think, having the connection opened to SQL database for 8 hours is a bad practise. Do you have some long-running operation, that requires so much time? Then consider having a stored procedure in your database, that should dump results into some temporary table, which you can check later.

But normally, the application should release the connection (and, ahh, all prepared statements with it) ASAP. To speedup the connection opening, you can use connection pooling (it should exist for ODBC as well). Connection pool will check that connection is "alive" before returning it to application, for example, see validationQuery parameter for Java DBCP.

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