Hello all. I have a little bit of a problem and I was hoping to send it out to the list for thought.
The setup:
I asked the list a little while ago about creating some code to check the DB to see if it was there before processing anything. Of course, if there is no problem the page
would load and porcess normally however, if for some reason the DB was not online, we have the option of: changing the data source to the backup DB (future) or displaying a nice
friendly message to the user that the DB was down and the admin staff has been notified.
It was suggested that we use and interceptor to accomplish this task. So far, this is working fine but I noticed an issue when I started testing this code.
First, let me give you a little peek into the directory structure:
/var/www/html/coldbox
/var/www/html/coldspring
/var/www/html/project1
/var/www/html/project2
/var/www/html/project3
/var/www/html/project4
/var/www/html/project5
The projects have their own coldbox structure inside so they are separate from each other and they don’t interact.
Example: /var/www/html/project1 has the following directories: config, handlers, includes, interceptors etc just like a “normal” CB project would.
This is an example of the interceptor that is present in all of the projects:
component extends=“coldbox.system.Interceptor” {
void function configure(){
}
function checkDatabase(event) {
var rc = event.getCollection();
rc.i = 0;
do {
try{
rc.i += 1;
qQuery = new Query();
//qQuery.setDatasource(“OURDBNAME”);
qSql = “select 1 from dual”;
qQuery.setSql(qSql);
result = qQuery.execute();
//metaInfo = result.getPrefix();
//records = result.getResult(); ;
} catch (any e) {
sleep(1000);
if(rc.i == 7) {
//writeDump(var=getSetting(“Environment”),abort=true);
rc.msg_type = ‘database’;
//We only want to send mail if the environment is PROD
if(getSetting(“Environment”) == “PRODUCTION”) {
// Get new mail object
mail = getPlugin(“MailService”).newMail().config(from=“webmaster@”,
to=“theadminstaff@”,
type=“html”,
subject=“Database Failure #application.application_name#”);
// create some tokens
tokens = {appname=application.application_name, date=dateformat(now(),“full”), time=timeformat(now(),“full”)};
mail.setBodyTokens(tokens);
// Set some body text
mail.setBody("
Application = @appname@
The Error Occured:@date@ @time@.
");
// Send the mail
results = getPlugin(“MailService”).send(mail);
}
//event.setView(“error”);
location("/project1/views/error.cfm?"&“msg_type=”&rc.msg_type);
}
//throw;
}
} while (rc.i <= 10);
}
}
Ok so, when everything is fine, this will not throw an error and everything is right with the world. To test this, I just went to the DSN on the CF Admin side and changed the user name of the so that the login would fail and it will simulate a DB problem. *Please note I changed the emails and the DB name for posting and left out the domain name. Also, in each respective project this line: location("/project1/views/error.cfm?"&“msg_type=”&rc.msg_type); changes with the project. So in project2 its: location("/project2/views/error.cfm?"&“msg_type=”&rc.msg_type); and so on.
Now, when this runs with the DB “broken” I will get an email if its on production and it will go to the error page informing the user that the DB is down and will land in the project1/views/error.cfm page
However, I have noticed that if I run this on project1, with the DB in a “broken” state, everything is fine, but if I then go and run this on project2 it will throw the error but always end up here: project1/views/error.cfm.
The same thing will happen with project3, 4 etc. This will not “fix” itself until I restart coldfusion and “fix” the DB. To make things even more confusing, say I make a “reset” of everything,
and then “break” the DB but go to project4 first. We will get the DB error and then end up here: project4/views/error.cfm and so will every other project that “trips” that error.
Does coldbox save some information or something that I don’t know about ? I have also been trying to follow this all the way with the debugger to see if I can detect the moment when the switch is made.
So far, I haven’t made it all the way through as there is a lot of coldbox code to step through or skip over.
If anyone has any suggestions or corrections or needs any other information, please let me know.
Thanks in advance!
Mallory