在继续之前检查实时数据源名称

发布于 2024-10-12 08:20:20 字数 194 浏览 3 评论 0原文

在继续处理该请求之前,让 CF 应用程序检查有效的数据库是否可以?

这是因为在某些情况下,数据库服务器可能会关闭或正在升级,因此在发出依赖于数据库的请求时会出现错误。

如果没有与数据库服务器的连接,用户可以安全地重定向到安全页面。

或者 cfcatch 可以工作吗?

如何进行这项检查?

谢谢。

Would it be ok to get a CF app to check for a valid database before proceeding to process that request?

This is because there may be instances where the database server may be down or being upgraded, hence an error comes when a db dependant request is made.

If there is no connection to the db server, the user can be safely redirected to a safe page.

Or can cfcatch work?

How can this check be done?

Thank you.

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

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

发布评论

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

评论(4

多孤肩上扛 2024-10-19 08:20:20

在 Application.cfc 文件或 Application.cfm 文件的 onRequestStart 方法中,您可以运行一个简单的查询来检查数据库是否可用。将查询包装在 cftry/cfcatch 中。如果查询失败,您可以在cfcatch中重定向用户,如果成功,您可以合理地确定您的数据库是“活动的”。

in your onRequestStart method of your Application.cfc file or in an Application.cfm file you can run a simple query to check that the database is available. Wrap the query in cftry/cfcatch. If the query fails, you can redirect the user in the cfcatch, if it succeeds, you can be reasonably sure that your database is "alive".

盛夏已如深秋| 2024-10-19 08:20:20

我在一个项目中使用过这样的检查。代码可能如下所示(不确定它是否可以在低于 8 的 ColdFusion 版本中工作),将此示例视为用 CFScript 编写的 UDF 块:

// service factory object instance
factory = CreateObject("java","coldfusion.server.ServiceFactory");
// the datasource service
dsService = factory.DatasourceService;
// verify the dsn
return dsService.verifyDataSource(arguments.dsn);

哦,我什至在我在旧笔记本电脑上编写的代码中发现了小注释几年前:

// [performance note] this server check takes 1-3ms at local PC (Kubuntu 7.10, CF8 + Apache2, Sempron 3500+, 1GB RAM)

虽然时间看起来很短,但我发现对每个请求进行此检查对于我的应用程序来说并不是真正有用。无论如何,我习惯广泛使用 try/catch 来处理错误。但如果您的数据源可能经常更改,那么它可能更有意义。

I've used such a check in one project. Code may looks as follows (not sure if it will work in versions of ColdFusion lower than 8), consider this sample as chunk of UDF written in CFScript:

// service factory object instance
factory = CreateObject("java","coldfusion.server.ServiceFactory");
// the datasource service
dsService = factory.DatasourceService;
// verify the dsn
return dsService.verifyDataSource(arguments.dsn);

Oh, I have even found small note in the code I wrote on my old laptop couple of years ago:

// [performance note] this server check takes 1-3ms at local PC (Kubuntu 7.10, CF8 + Apache2, Sempron 3500+, 1GB RAM)

While time looks like small I have found out that doing this check on each request is not really useful for my application. Any way I have a habit to use the try/catch extensively for errors handling. But if your datasources may cheange frequently it may have more sense.

超可爱的懒熊 2024-10-19 08:20:20

向每个请求添加额外的查询以确保数据库正常运行显然是一个坏主意。更好的方法是在您的应用程序中构建一个“维护模式”开关,您可以在进行计划维护(升级等)时手动启用该开关。

如果您希望在发生错误(例如数据库问题)时显示“友好”页面,请使用 onError() Application.cfc 和/或 标记在 Application.cfm 中,作为全局错误处理程序。

Adding an extra query to every request to make sure that the database is up is a patently bad idea. A better approach would be to build a "maintenance mode" switch into your application, that you would manually enable when you are doing planned maintenance (upgrades, etc).

If you want to have a "friendly" page displayed when an error (like database issues) occur, then use the onError() method in Application.cfc and/or the <cferror .../> tag in Application.cfm, as a global error handler.

白鸥掠海 2024-10-19 08:20:20

如果您担心数据库可能会消失,我会在您的 OnRequestStart 处理程序中实现一个“SELECT 1 AS A”查询,该查询仅每 N 分钟运行一次。这可以通过使用查询缓存功能来完成。我首先每 30 分钟执行一次查询。

If you are worried the db could vanish, I would implement a "SELECT 1 AS A" query in your OnRequestStart handler that runs only every N minutes. This can be accomplished by using the query caching feature. I'd start with performing the query every 30 min.

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