Check for live Data Source Name Before proceeding
Asked Answered
L

4

5

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.

Linalinacre answered 15/1, 2011 at 19:47 Comment(0)
B
5

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".

Brilliantine answered 15/1, 2011 at 20:25 Comment(0)
S
2

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.

Shawana answered 15/1, 2011 at 20:26 Comment(1)
This is a great solution, however, you should know that use of the service factory is technically undocumented and could be removed in later versions of ColdFusion. A similar, yet documented approach would be to use the AdminAPI. You would essentially do the same thing (verify the datasource), but in a supported way.Brilliantine
H
2

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.

Herbie answered 15/1, 2011 at 21:45 Comment(0)
E
0

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.

Eryneryngo answered 16/1, 2011 at 0:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.