Checking the state of a database connection
How does one check to see if a database connection is up and running? What I want to do is check to see if the connector is connected. If not, redirect the browser to another page. I tried a try/try but didn't seem to catch the exception. Also, can you put a redirect command in the standard_html_header? I tried this and it was ignored. ------- Regards, Graham Chiu gchiu<at>compkarori.co.nz http://www.compkarori.com/dynamo - The Homebuilt Dynamo http://www.compkarori.com/dbase - The dBase bulletin
Try this, where Database is the name of your database object <dtml-if "Database.connected()"> Yes <dtml-else> No </dtml-if> DR
"Daniel G. Rusch" wrote:
Try this, where Database is the name of your database object <dtml-if "Database.connected()"> Yes <dtml-else> No </dtml-if> I use ZPyGreSQLDA. The function connected() returns the value self._v_connected (DateTime of last connect call) of the Connection object. So if the SQL server crashes while Zope is still working the result of connected() is still 'true' although a query will fail :-(
_________________________________________________________ Andreas Heckel andreas@aHIT.de
Andreas wrote:
"Daniel G. Rusch" wrote:
Try this, where Database is the name of your database object <dtml-if "Database.connected()"> Yes <dtml-else> No </dtml-if> I use ZPyGreSQLDA. The function connected() returns the value self._v_connected (DateTime of last connect call) of the Connection object. So if the SQL server crashes while Zope is still working the result of connected() is still 'true' although a query will fail :-(
I have modified UserDB to just do close/open on the connection if the query fails. You could do something similar - by doing a dummy query (like "select 1") and checking if it succeeds. -------- Hannu
In article <38903AD4.C7E84C99@tm.ee>, Hannu Krosing <hannu@tm.ee> writes
I have modified UserDB to just do close/open on the connection if the query fails.
Unfortunately, if I lose my ODBC connection, more often than not I have to shut Zope down, and restart it to reconnect the ODBC adapter. Otherwise it complains of an invalid log on string. ------- Regards, Graham Chiu gchiu<at>compkarori.co.nz http://www.compkarori.com/dynamo - The Homebuilt Dynamo http://www.compkarori.com/dbase - The dBase bulletin
Hannu Krosing writes I have modified UserDB to just do close/open on the connection if the query fails. ... and if the SQL server still is down ... ;-)
Unfortunately, if I lose my ODBC connection, more often than not I have to shut Zope down, and restart it to reconnect the ODBC adapter. Otherwise it complains of an invalid log on string.
This solution works fine (only tested with Postgres) and you do not need to restart Zope. Anyway it's only a work around because IMHO Database.connected() should not simply return the date of last successful connect call. <!--#try--> <!--#call "Database.connect(Database.connection_string)"--> <!--#if "Database.connected()"--> Let´s do some query :-) <!--#else--> Database connection object not connected :-( <!--#/if--> <!--#except BadRequest--> Possibly SQL server down :-( <BR> <!--#var error_type--> <!--#var error_value--> <!--#/try--> _________________________________________________________ Andreas Heckel andreas@aHIT.de
I have run into this problem also so this is what we do (sorry for not elaborating in my first post, wanted to keep it simple): <dtml-if "Database.connected()"> <dtml-try> <dtml-call DbTestMethod> OK <dtml-except> Database Connection or Oracle is down </dtml-try> <dtml-else> Database Connection or Oracle is down </dtml-if> sql in DbTestMethod: select SYSDATE from DUAL
participants (4)
-
Andreas -
Daniel G. Rusch -
Graham Chiu -
Hannu Krosing