diff --git a/bin/sql_db.py b/bin/sql_db.py index 1e88f1b66c7..f3b8c86b64c 100644 --- a/bin/sql_db.py +++ b/bin/sql_db.py @@ -71,7 +71,7 @@ class Cursor(object): @wraps(f) def wrapper(self, *args, **kwargs): - if not hasattr(self, '_obj'): + if self.__closed: raise psycopg2.ProgrammingError('Unable to use the cursor after having closing it') return f(self, *args, **kwargs) return wrapper @@ -81,6 +81,7 @@ class Cursor(object): self._serialized = serialized self._cnx = pool.getconn() self._obj = self._cnx.cursor(cursor_factory=psycopg1cursor) + self.__closed = False self.autocommit(False) self.dbname = pool.dbname @@ -89,7 +90,7 @@ class Cursor(object): self.__caller = tuple(stack()[2][1:3]) def __del__(self): - if hasattr(self, '_obj'): + if not self.__closed: if tools.config['log_level'] in (netsvc.LOG_DEBUG, netsvc.LOG_DEBUG_RPC): # Oops. 'self' has not been closed explicitly. # The cursor will be deleted by the garbage collector, @@ -166,6 +167,7 @@ class Cursor(object): # collected as fast as they should). The problem is probably due in # part because browse records keep a reference to the cursor. del self._obj + self.__closed = True self._pool.putconn(self._cnx) @check