在继续之前检查实时数据源名称
在继续处理该请求之前,让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 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".
我在一个项目中使用过这样的检查。代码可能如下所示(不确定它是否可以在低于 8 的 ColdFusion 版本中工作),将此示例视为用 CFScript 编写的 UDF 块:
哦,我什至在我在旧笔记本电脑上编写的代码中发现了小注释几年前:
虽然时间看起来很短,但我发现对每个请求进行此检查对于我的应用程序来说并不是真正有用。无论如何,我习惯广泛使用 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:
Oh, I have even found small note in the code I wrote on my old laptop couple of years ago:
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.
向每个请求添加额外的查询以确保数据库正常运行显然是一个坏主意。更好的方法是在您的应用程序中构建一个“维护模式”开关,您可以在进行计划维护(升级等)时手动启用该开关。
如果您希望在发生错误(例如数据库问题)时显示“友好”页面,请使用
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.如果您担心数据库可能会消失,我会在您的 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.