[DB-SIG] how to know the connexion is closed

William Dode wilk-ml at flibuste.net
Tue Apr 25 11:00:17 CEST 2006


On 24-04-2006, Bill Campbell wrote:
> On Mon, Apr 24, 2006, Dieter Maurer wrote:
>>William Dode wrote at 2006-4-22 18:53 +0000:
>>>I search for a generic way (for psycopg, mysqldb, adodbapi) to
>>>know that the connexion is closed
>>
>>I fear there is no generic way....
>
> One could use something like:
>
> try: cur = conn.cursor()
> except: cur = None
>
> I would think that attempting to get a cursor from a closed
> connection would always raise an error.

Not with MySQLdb :
>>> cx = MySQLdb.connect(db='scrabble')
>>> cx.close()
>>> cx.cursor()
<MySQLdb.cursors.Cursor object at 0xb7dc1d8c>
(and a query fail with InterfaceError)

with sqlite it raise ProgrammingError
>>> cx = sqlite.connect(':memory:')
>>> cx.close()
>>> cx.cursor()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.3/site-packages/sqlite/main.py", line 552, in cursor
    self._checkNotClosed("cursor")
  File "/usr/lib/python2.3/site-packages/sqlite/main.py", line 482, in _checkNotClosed
    raise _sqlite.ProgrammingError, \
_sqlite.ProgrammingError: cursor failed - Connection is closed.

with psycopg it raise InterfaceError
>>> import psycopg
>>> cx = psycopg.connect('dbname=pytodo')
>>> cx.close()
>>> cx.cursor()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
psycopg.InterfaceError: already closed


Same problem if i try to close a connection already closed
MySQLdb and sqlite raise ProgrammingError
psycopg raise InterfaceError

Don't you think it should be a spec in dbapi ?

-- 
William Dodé - http://flibuste.net



More information about the DB-SIG mailing list