[MERGE] backport from stable
lp bug: https://launchpad.net/bugs/488234 fixed bzr revid: chs@tinyerp.com-20091128001554-9ka8o40104a81bj7
This commit is contained in:
parent
fda2b950eb
commit
97413f1eeb
|
@ -52,6 +52,7 @@ psycopg2.extensions.register_type(psycopg2.extensions.new_type((700, 701, 1700,)
|
||||||
import tools
|
import tools
|
||||||
from tools.func import wraps
|
from tools.func import wraps
|
||||||
from datetime import datetime as mdt
|
from datetime import datetime as mdt
|
||||||
|
from datetime import timedelta
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
@ -133,18 +134,21 @@ class Cursor(object):
|
||||||
raise
|
raise
|
||||||
|
|
||||||
if self.sql_log:
|
if self.sql_log:
|
||||||
|
delay = mdt.now() - now
|
||||||
|
delay = delay.seconds * 1E6 + delay.microseconds
|
||||||
|
|
||||||
log("query: %s" % self._obj.query)
|
log("query: %s" % self._obj.query)
|
||||||
self.sql_log_count+=1
|
self.sql_log_count+=1
|
||||||
res_from = re_from.match(query.lower())
|
res_from = re_from.match(query.lower())
|
||||||
if res_from:
|
if res_from:
|
||||||
self.sql_from_log.setdefault(res_from.group(1), [0, 0])
|
self.sql_from_log.setdefault(res_from.group(1), [0, 0])
|
||||||
self.sql_from_log[res_from.group(1)][0] += 1
|
self.sql_from_log[res_from.group(1)][0] += 1
|
||||||
self.sql_from_log[res_from.group(1)][1] += mdt.now() - now
|
self.sql_from_log[res_from.group(1)][1] += delay
|
||||||
res_into = re_into.match(query.lower())
|
res_into = re_into.match(query.lower())
|
||||||
if res_into:
|
if res_into:
|
||||||
self.sql_into_log.setdefault(res_into.group(1), [0, 0])
|
self.sql_into_log.setdefault(res_into.group(1), [0, 0])
|
||||||
self.sql_into_log[res_into.group(1)][0] += 1
|
self.sql_into_log[res_into.group(1)][0] += 1
|
||||||
self.sql_into_log[res_into.group(1)][1] += mdt.now() - now
|
self.sql_into_log[res_into.group(1)][1] += delay
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def print_log(self):
|
def print_log(self):
|
||||||
|
@ -160,10 +164,12 @@ class Cursor(object):
|
||||||
sqllogitems.sort(key=lambda k: k[1][1])
|
sqllogitems.sort(key=lambda k: k[1][1])
|
||||||
log("SQL LOG %s:" % (type,))
|
log("SQL LOG %s:" % (type,))
|
||||||
for r in sqllogitems:
|
for r in sqllogitems:
|
||||||
log("table: %s: %s/%s" %(r[0], str(r[1][1]), r[1][0]))
|
delay = timedelta(microseconds=r[1][1])
|
||||||
|
log("table: %s: %s/%s" %(r[0], str(delay), r[1][0]))
|
||||||
sum+= r[1][1]
|
sum+= r[1][1]
|
||||||
sqllogs[type].clear()
|
sqllogs[type].clear()
|
||||||
log("SUM %s:%s/%d [%d]" % (type, sum, self.sql_log_count,sql_counter))
|
sum = timedelta(microseconds=sum)
|
||||||
|
log("SUM %s:%s/%d [%d]" % (type, str(sum), self.sql_log_count, sql_counter))
|
||||||
process('from')
|
process('from')
|
||||||
process('into')
|
process('into')
|
||||||
self.sql_log_count = 0
|
self.sql_log_count = 0
|
||||||
|
@ -240,7 +246,7 @@ class ConnectionPool(object):
|
||||||
|
|
||||||
result = None
|
result = None
|
||||||
for i, (cnx, used) in enumerate(self._connections):
|
for i, (cnx, used) in enumerate(self._connections):
|
||||||
if not used and cnx.dsn == dsn:
|
if not used and dsn_are_equals(cnx.dsn, dsn):
|
||||||
self._debug('Existing connection found at index %d' % i)
|
self._debug('Existing connection found at index %d' % i)
|
||||||
|
|
||||||
self._connections.pop(i)
|
self._connections.pop(i)
|
||||||
|
@ -281,7 +287,7 @@ class ConnectionPool(object):
|
||||||
@locked
|
@locked
|
||||||
def close_all(self, dsn):
|
def close_all(self, dsn):
|
||||||
for i, (cnx, used) in tools.reverse_enumerate(self._connections):
|
for i, (cnx, used) in tools.reverse_enumerate(self._connections):
|
||||||
if cnx.dsn == dsn:
|
if dsn_are_equals(cnx.dsn, dsn):
|
||||||
cnx.close()
|
cnx.close()
|
||||||
self._connections.pop(i)
|
self._connections.pop(i)
|
||||||
|
|
||||||
|
@ -300,6 +306,7 @@ class Connection(object):
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
if self._unique:
|
if self._unique:
|
||||||
|
close_db(self.dbname)
|
||||||
self.__LOCKS[self.dbname].release()
|
self.__LOCKS[self.dbname].release()
|
||||||
|
|
||||||
def cursor(self, serialized=False):
|
def cursor(self, serialized=False):
|
||||||
|
@ -318,6 +325,13 @@ for p in ('host', 'port', 'user', 'password'):
|
||||||
def dsn(db_name):
|
def dsn(db_name):
|
||||||
return '%sdbname=%s' % (_dsn, db_name)
|
return '%sdbname=%s' % (_dsn, db_name)
|
||||||
|
|
||||||
|
def dsn_are_equals(first, second):
|
||||||
|
def key(dsn):
|
||||||
|
k = dict(x.split('=', 1) for x in dsn.strip().split())
|
||||||
|
k.pop('password', None) # password is not relevant
|
||||||
|
return k
|
||||||
|
return key(first) == key(second)
|
||||||
|
|
||||||
|
|
||||||
_Pool = ConnectionPool(int(tools.config['db_maxconn']))
|
_Pool = ConnectionPool(int(tools.config['db_maxconn']))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue