[IMP] sql_db.py: improve logging
bzr revid: chs@openerp.com-20100514133341-y093gp9yqfckcm80
This commit is contained in:
parent
7b9fb87c01
commit
028ee0f221
|
@ -118,7 +118,7 @@ class Cursor(object):
|
||||||
params = params or None
|
params = params or None
|
||||||
res = self._obj.execute(query, params)
|
res = self._obj.execute(query, params)
|
||||||
except psycopg2.ProgrammingError, pe:
|
except psycopg2.ProgrammingError, pe:
|
||||||
self.__logger.error("Programming error: %s, in query %s" % (pe, query))
|
self.__logger.error("Programming error: %s, in query %s", pe, query)
|
||||||
raise
|
raise
|
||||||
except Exception:
|
except Exception:
|
||||||
self.__logger.exception("bad query: %s", self._obj.query)
|
self.__logger.exception("bad query: %s", self._obj.query)
|
||||||
|
@ -250,13 +250,13 @@ class ConnectionPool(object):
|
||||||
count = len(self._connections)
|
count = len(self._connections)
|
||||||
return "ConnectionPool(used=%d/count=%d/max=%d)" % (used, count, self._maxconn)
|
return "ConnectionPool(used=%d/count=%d/max=%d)" % (used, count, self._maxconn)
|
||||||
|
|
||||||
def _debug(self, msg):
|
def _debug(self, msg, *args):
|
||||||
self.__logger.debug(repr(self))
|
msg = '%r ' + msg
|
||||||
self.__logger.debug(msg)
|
self.__logger.debug(msg, self, *args)
|
||||||
|
|
||||||
@locked
|
@locked
|
||||||
def borrow(self, dsn):
|
def borrow(self, dsn):
|
||||||
self._debug('Borrow connection to %s' % (dsn,))
|
self._debug('Borrow connection to %r', dsn)
|
||||||
|
|
||||||
# free leaked connections
|
# free leaked connections
|
||||||
for i, (cnx, _) in tools.reverse_enumerate(self._connections):
|
for i, (cnx, _) in tools.reverse_enumerate(self._connections):
|
||||||
|
@ -264,13 +264,13 @@ class ConnectionPool(object):
|
||||||
delattr(cnx, 'leaked')
|
delattr(cnx, 'leaked')
|
||||||
self._connections.pop(i)
|
self._connections.pop(i)
|
||||||
self._connections.append((cnx, False))
|
self._connections.append((cnx, False))
|
||||||
self._debug('Free leaked connection to %s' % (cnx.dsn,))
|
self._debug('Free leaked connection to %r', cnx.dsn)
|
||||||
|
|
||||||
for i, (cnx, used) in enumerate(self._connections):
|
for i, (cnx, used) in enumerate(self._connections):
|
||||||
if not used and dsn_are_equals(cnx.dsn, dsn):
|
if not used and dsn_are_equals(cnx.dsn, dsn):
|
||||||
self._connections.pop(i)
|
self._connections.pop(i)
|
||||||
self._connections.append((cnx, True))
|
self._connections.append((cnx, True))
|
||||||
self._debug('Existing connection found at index %d' % i)
|
self._debug('Existing connection found at index %d', i)
|
||||||
|
|
||||||
return cnx
|
return cnx
|
||||||
|
|
||||||
|
@ -279,7 +279,7 @@ class ConnectionPool(object):
|
||||||
for i, (cnx, used) in enumerate(self._connections):
|
for i, (cnx, used) in enumerate(self._connections):
|
||||||
if not used:
|
if not used:
|
||||||
self._connections.pop(i)
|
self._connections.pop(i)
|
||||||
self._debug('Removing old connection at index %d: %s' % (i, cnx.dsn))
|
self._debug('Removing old connection at index %d: %r', i, cnx.dsn)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
# note: this code is called only if the for loop has completed (no break)
|
# note: this code is called only if the for loop has completed (no break)
|
||||||
|
@ -292,20 +292,22 @@ class ConnectionPool(object):
|
||||||
|
|
||||||
@locked
|
@locked
|
||||||
def give_back(self, connection, keep_in_pool=True):
|
def give_back(self, connection, keep_in_pool=True):
|
||||||
self._debug('Give back connection to %s' % (connection.dsn,))
|
self._debug('Give back connection to %r', connection.dsn)
|
||||||
for i, (cnx, used) in enumerate(self._connections):
|
for i, (cnx, used) in enumerate(self._connections):
|
||||||
if cnx is connection:
|
if cnx is connection:
|
||||||
self._connections.pop(i)
|
self._connections.pop(i)
|
||||||
if keep_in_pool:
|
if keep_in_pool:
|
||||||
self._connections.append((cnx, False))
|
self._connections.append((cnx, False))
|
||||||
self._debug('Put connection to %s in pool' % (cnx.dsn,))
|
self._debug('Put connection to %r in pool', cnx.dsn)
|
||||||
|
else:
|
||||||
|
self._debug('Forgot connection to %r', cnx.dsn)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
raise PoolError('This connection does not below to the pool')
|
raise PoolError('This connection does not below to the pool')
|
||||||
|
|
||||||
@locked
|
@locked
|
||||||
def close_all(self, dsn):
|
def close_all(self, dsn):
|
||||||
self._debug('Close all connections to %s' % (dsn,))
|
self._debug('Close all connections to %r', dsn)
|
||||||
for i, (cnx, used) in tools.reverse_enumerate(self._connections):
|
for i, (cnx, used) in tools.reverse_enumerate(self._connections):
|
||||||
if dsn_are_equals(cnx.dsn, dsn):
|
if dsn_are_equals(cnx.dsn, dsn):
|
||||||
cnx.close()
|
cnx.close()
|
||||||
|
@ -321,7 +323,7 @@ class Connection(object):
|
||||||
|
|
||||||
def cursor(self, serialized=False):
|
def cursor(self, serialized=False):
|
||||||
cursor_type = serialized and 'serialized ' or ''
|
cursor_type = serialized and 'serialized ' or ''
|
||||||
self.__logger.debug('create %scursor to "%s"' % (cursor_type, self.dbname,))
|
self.__logger.debug('create %scursor to %r', cursor_type, self.dbname)
|
||||||
return Cursor(self._pool, self.dbname, serialized=serialized)
|
return Cursor(self._pool, self.dbname, serialized=serialized)
|
||||||
|
|
||||||
def serialized_cursor(self):
|
def serialized_cursor(self):
|
||||||
|
|
Loading…
Reference in New Issue