I want to create some blocks that query an external database.
Per instructions on http://drupal.org/node/18429, I have been able to successfully do this as long as the connection is working. Since the database is on a different server, sometimes the connection may be broken. If the connection is broken, I get a big, ugly error page that kills the entire page.
My question is: how can I gracefully check to see that the connection to the secondary database is working and show a nice, friendly "sorry, this database isn't available", rather than a big giant error that breaks everything??
I looked at the DB abstraction layer page at http://api.drupal.org/api/group/database and couldn't find anything that works. I presume this is because the connection is defined in settings.php and the array variable exists for my block code to proceed even though the actual connection is broken.
Help!?!?
| Comment | File | Size | Author |
|---|---|---|---|
| #19 | drupal-496184-19-D6.patch | 4 KB | mikeytown2 |
| #18 | pressflow-496184-18-D6.patch | 6 KB | mikeytown2 |
| #17 | pressflow-496184-15-D6.patch | 5.52 KB | mikeytown2 |
| #15 | drupal-496184-15.patch | 4 KB | mikeytown2 |
| #10 | drupal-496184_1.patch | 3.55 KB | mikeytown2 |
Comments
Comment #1
emackn commentedWould be nice if there was a built in function.
But there isn't so I did this:
It's not perfect, but might help. Also, it assumes you are connecting to another Drupal database, but you could make appropriate changes if you needed. Maybe add a custom table to your "other" database that does not exist on the default one.
Comment #2
isaac77 commentedThanks very much for the suggestion emackn, but the workaround you propose does not help me.
It seems that the db_set_active() call itself causes the _db_error_page to be returned, before we can even get to the test that checks if the current site name matches the "otherDB" site name.
Any other suggestions out there? It does not seems right that a connection problem to an external database must produce an error page, rather than returning a value that can be tested and acted on. Do we have to make do and write something with php functions for now?
Comment #3
gpk commented>It does not seems right that a connection problem to an external database must produce an error page
Unfortunately (for you) that is the way it works at the moment. You would probably need to write a customized version of db_connect() (see http://api.drupal.org/api/drupal/includes--database.mysql.inc/6/source) and maybe also of http://api.drupal.org/api/function/db_set_active/6 so that if you can't connect then you can do something other than show the DB error page. Then invoke these from the block. Or put the whole lot into a small custom module.
Comment #4
inders commentedA module for such external database functionalty is prefered.
-Inder Singh
http://indersingh.com
Comment #5
mikeytown2 commentedSubscribe.
Problem is in db_connect()
_db_error_page() then aborts everything with an exit. I think the best option ATM would be to add in a db_validate function as this wouldn't change the old behavior.
Comment #6
mikeytown2 commenteddb_validate would have duplicated a lot of code, I think this is a better way.
This adds a new input to db_set_active; the $abort variable. It's set to TRUE by default. Example code on how to use it
Comment #7
mikeytown2 commentedComment #8
mikeytown2 commented$abort is not set
Comment #9
mikeytown2 commented$abort is set at the top of the function. Anyway made this clearer and added in comments. Note that this is 100% backwards compatible.
Comment #10
mikeytown2 commentedComment #12
mikeytown2 commentedComment #13
willeaton commentedI'd like to bump this please. This is the perfect solution to my problem. I connect to 4 different databases in quite a large website as it requires integration with other systems. These systems are not critical so I don't really care if one or two of them fail. The problem is, if one of them fails then it drops the entire website which is not desirable at all.
We need a way to set if the database connection is critical or not, and the ability to include $abort in the connection is exactly what we are after
Comment #14
mikeytown2 commented@willeaton
we use this patch quite successfully to access 70+ databases over 3 different mysql servers.
Comment #15
mikeytown2 commentedupdated patch
Comment #17
mikeytown2 commentedPatch for pressflow
Comment #18
mikeytown2 commentedRe-rolled against pressflow 6.25
Comment #19
mikeytown2 commentedPatch against core
Comment #20
jelo commentedHas this been committed to core? I would like to see this functionality added as well...
Comment #21
mikeytown2 commentedThis has not been committed to core and I don't think it will be as there is not enough interest here.
Comment #22
jelo commentedWell, I am not sure how the Drupal community normally handles this, but "interest" should not be the deciding factor if a critical bug has been identified and there is a fix for it. In essence, Drupal positions itself more and more as a framework that can integrate with and into various other systems. To me, being able to access multiple databases is at the essence of some of this. It sounds like a very bad idea to me to let a whole site break if for whatever reason a secondary database is not available. Could you please advice what needs to be done to get this committed?
Comment #23
mikeytown2 commented@jelo
Have you tested the patch (#19) on your setup? Does it do what you need it to? If so say that the patch works and we can then look at moving the status from "needs review" to "reviewed and tested by the community". If the patch doesn't do what you need, say what needs to be changed and change the status to "needs work". Right now the patch has been tested by me, and it works great in my case (1400 domains connecting to 80 databases; one massive multisite). It needs to be vetted by more people before it has a shot of getting into core.
Comment #24
mikeytown2 commented