self._cnx = pool.borrow(dsn(dbname))
self._obj = self._cnx.cursor(cursor_factory=psycopg1cursor)
- self.__closed = False # real initialisation value
- self.autocommit(False)
if self.sql_log:
self.__caller = frame_codeinfo(currentframe(),2)
else:
self.__caller = False
+ self.__closed = False # real initialisation value
+ self.autocommit(False)
self.__closer = False
+ self._default_log_exceptions = True
+
def __del__(self):
- if not self.__closed:
+ if not self.__closed and not self._cnx.closed:
# Oops. 'self' has not been closed explicitly.
# The cursor will be deleted by the garbage collector,
# but the database connection is not put back into the connection
self._close(True)
@check
- def execute(self, query, params=None, log_exceptions=True):
+ def execute(self, query, params=None, log_exceptions=None):
if '%d' in query or '%f' in query:
self.__logger.warn(query)
self.__logger.warn("SQL queries cannot contain %d or %f anymore. "
params = params or None
res = self._obj.execute(query, params)
except psycopg2.ProgrammingError, pe:
- if log_exceptions:
+ if (self._default_log_exceptions if log_exceptions is None else log_exceptions):
self.__logger.error("Programming error: %s, in query %s", pe, query)
raise
except Exception:
- if log_exceptions:
+ if (self._default_log_exceptions if log_exceptions is None else log_exceptions):
self.__logger.exception("bad query: %s", self._obj.query or query)
raise
del self._obj
self.__closed = True
+ # Clean the underlying connection.
+ self._cnx.rollback()
+
if leak:
self._cnx.leaked = True
else:
# free leaked connections
for i, (cnx, _) in tools.reverse_enumerate(self._connections):
+ if cnx.closed:
+ self._connections.pop(i)
+ self._debug('Removing closed connection at index %d: %r', i, cnx.dsn)
+ continue
if getattr(cnx, 'leaked', False):
delattr(cnx, 'leaked')
self._connections.pop(i)
self._debug('Put connection to %r in pool', cnx.dsn)
else:
self._debug('Forgot connection to %r', cnx.dsn)
+ cnx.close()
break
else:
raise PoolError('This connection does not below to the pool')
return Connection(_Pool, db_name)
def close_db(db_name):
- """ You might want to call openerp.netsvc.Agent.cancel(db_name) along this function."""
+ """ You might want to call openerp.modules.registry.RegistryManager.delete(db_name) along this function."""
_Pool.close_all(dsn(db_name))
- tools.cache.clean_caches_for_db(db_name)
ct = currentThread()
if hasattr(ct, 'dbname'):
delattr(ct, 'dbname')