test connection

795 views
Skip to first unread message

ch1zra

unread,
Jun 1, 2010, 1:38:09 AM6/1/10
to pyodbc
is there any way to test the connection, before I try everything
else ?
apparently, people in my company for some reason prefer to shutdown
servers at random times, and I'd like to check if the connection
exists, inform the user and then proceed with other operations.

i tried
if not cnxn:
print 'no conn found.'

but it's getting me nowhere..

thanx in advance :)

mkleehammer

unread,
Jun 1, 2010, 11:22:58 PM6/1/10
to pyodbc
If you are using Windows, ODBC connection pooling should do this
automatically for SQL Server. (That is, I believe the SQL Server
driver checks connections when requested from the ODBC pool.) IIRC,
you should search for "SQLServer" "sp_reset" for more information
about this.

Otherwise, there isn't anything built-in to ODBC, so the thing to do
is to send the fastest message possible from your connection pool
code, something like "SELECT 1" (SQL Server) or "SELECT 1 FROM
DUAL" (Oracle).

If you are using PostgreSQL, I've heard good things about pgbounce
which does this for you. You conect to pgbounce and it is responsible
for serving good connections.

Not a great answer, I know, but I hope it points you in the right
direction.

ch1zra

unread,
Jun 8, 2010, 1:34:40 AM6/8/10
to pyodbc
i madeit this way:
try:
cnxn = pyodbc.connect("DRIVER={Microsoft ODBC for
Oracle};UID=UNAME;PWD=PASS;SERVER=SERV;")
except:
print 'No connection found!'

and then the rest of code and all that follows :)
Reply all
Reply to author
Forward
0 new messages