summaryrefslogtreecommitdiff
path: root/bin/sql_db.py
diff options
context:
space:
mode:
authorHDA (OpenERP) <hda@tinyerp.com>2010-02-16 12:01:14 +0530
committerHDA (OpenERP) <hda@tinyerp.com>2010-02-16 12:01:14 +0530
commita0ba43d80060719692c9ad784994606c8e7a33e3 (patch)
tree788917876daa26e9d68f62b08134e3d1d3e20485 /bin/sql_db.py
parent5c8360f9999872f4d8be6938cfe91dcea3f01b01 (diff)
parent1d5272071d7af6c0680a952cfaa6635cdffacf32 (diff)
Merged with stable
bzr revid: hda@tinyerp.com-20100216063114-d2crv3zayarq2vk6
Diffstat (limited to 'bin/sql_db.py')
-rw-r--r--bin/sql_db.py75
1 files changed, 27 insertions, 48 deletions
diff --git a/bin/sql_db.py b/bin/sql_db.py
index dc462526049..2cc71408f5c 100644
--- a/bin/sql_db.py
+++ b/bin/sql_db.py
@@ -54,6 +54,7 @@ from tools.func import wraps
from datetime import datetime as mdt
from datetime import timedelta
import threading
+from inspect import stack
import re
re_from = re.compile('.* from "?([a-zA-Z_0-9]+)"? .*$');
@@ -91,10 +92,7 @@ class Cursor(object):
self._obj = self._cnx.cursor(cursor_factory=psycopg1cursor)
self.__closed = False # real initialisation value
self.autocommit(False)
-
- if tools.config['log_level'] in (netsvc.LOG_DEBUG, netsvc.LOG_DEBUG_RPC):
- from inspect import stack
- self.__caller = tuple(stack()[2][1:3])
+ self.__caller = tuple(stack()[2][1:3])
def __del__(self):
if not self.__closed:
@@ -103,10 +101,9 @@ class Cursor(object):
# but the database connection is not put back into the connection
# pool, preventing some operation on the database like dropping it.
# This can also lead to a server overload.
- if tools.config['log_level'] in (netsvc.LOG_DEBUG, netsvc.LOG_DEBUG_RPC):
- msg = "Cursor not closed explicitly\n" \
- "Cursor was created at %s:%s" % self.__caller
- log(msg, netsvc.LOG_WARNING)
+ msg = "Cursor not closed explicitly\n" \
+ "Cursor was created at %s:%s" % self.__caller
+ log(msg, netsvc.LOG_WARNING)
self.close()
@check
@@ -119,7 +116,7 @@ class Cursor(object):
if self.sql_log:
now = mdt.now()
-
+
try:
params = params or None
res = self._obj.execute(query, params)
@@ -200,11 +197,11 @@ class Cursor(object):
def autocommit(self, on):
offlevel = [ISOLATION_LEVEL_READ_COMMITTED, ISOLATION_LEVEL_SERIALIZABLE][bool(self._serialized)]
self._cnx.set_isolation_level([offlevel, ISOLATION_LEVEL_AUTOCOMMIT][bool(on)])
-
+
@check
def commit(self):
return self._cnx.commit()
-
+
@check
def rollback(self):
return self._cnx.rollback()
@@ -233,16 +230,18 @@ class ConnectionPool(object):
self._lock = threading.Lock()
self._logger = netsvc.Logger()
- def _log(self, msg):
- #self._logger.notifyChannel('ConnectionPool', netsvc.LOG_INFO, msg)
- pass
+ def __repr__(self):
+ used = len([1 for c, u in self._connections[:] if u])
+ count = len(self._connections)
+ return "ConnectionPool(used=%d/count=%d/max=%d)" % (used, count, self._maxconn)
+
def _debug(self, msg):
- #self._logger.notifyChannel('ConnectionPool', netsvc.LOG_DEBUG, msg)
- pass
+ self._logger.notifyChannel('ConnectionPool', netsvc.LOG_DEBUG, repr(self))
+ self._logger.notifyChannel('ConnectionPool', netsvc.LOG_DEBUG, msg)
@locked
def borrow(self, dsn):
- self._log('Borrow connection to %s' % (dsn,))
+ self._debug('Borrow connection to %s' % (dsn,))
result = None
for i, (cnx, used) in enumerate(self._connections):
@@ -258,7 +257,7 @@ class ConnectionPool(object):
return result
if len(self._connections) >= self._maxconn:
- # try to remove the older connection not used
+ # try to remove the oldest connection not used
for i, (cnx, used) in enumerate(self._connections):
if not used:
self._debug('Removing old connection at index %d: %s' % (i, cnx.dsn))
@@ -266,7 +265,7 @@ class ConnectionPool(object):
break
else:
# note: this code is called only if the for loop has completed (no break)
- raise PoolError('Connection Pool Full')
+ raise PoolError('The Connection Pool Is Full')
self._debug('Create new connection')
result = psycopg2.connect(dsn=dsn)
@@ -275,7 +274,7 @@ class ConnectionPool(object):
@locked
def give_back(self, connection):
- self._log('Give back connection to %s' % (connection.dsn,))
+ self._debug('Give back connection to %s' % (connection.dsn,))
for i, (cnx, used) in enumerate(self._connections):
if cnx is connection:
self._connections.pop(i)
@@ -286,6 +285,7 @@ class ConnectionPool(object):
@locked
def close_all(self, dsn):
+ self._debug('Close all connections to %s' % (dsn,))
for i, (cnx, used) in tools.reverse_enumerate(self._connections):
if dsn_are_equals(cnx.dsn, dsn):
cnx.close()
@@ -293,37 +293,17 @@ class ConnectionPool(object):
class Connection(object):
- __LOCKS = {}
+ def _debug(self, msg):
+ self._logger.notifyChannel('Connection', netsvc.LOG_DEBUG, msg)
- def __init__(self, pool, dbname, unique=False):
+ def __init__(self, pool, dbname):
self.dbname = dbname
self._pool = pool
- self._unique = unique
-
- def __enter__(self):
- if self._unique:
- self.lock()
- return self
-
- def __exit__(self, exc_type, exc_value, traceback):
- if self._unique:
- self.release()
-
- def lock(self):
- if self.dbname not in self.__LOCKS:
- self.__LOCKS[self.dbname] = threading.Lock()
- self.__LOCKS[self.dbname].acquire()
-
- def release(self):
- close_db(self.dbname)
- self.__LOCKS[self.dbname].release()
+ self._logger = netsvc.Logger()
def cursor(self, serialized=False):
- if self._unique:
- lock = self.__LOCKS.get(self.dbname, None)
- if not (lock and lock.locked()):
- netsvc.Logger().notifyChannel('Connection', netsvc.LOG_WARNING, 'Unprotected connection to %s' % (self.dbname,))
-
+ cursor_type = serialized and 'serialized ' or ''
+ self._debug('create %scursor to "%s"' % (cursor_type, self.dbname,))
return Cursor(self._pool, self.dbname, serialized=serialized)
def serialized_cursor(self):
@@ -359,8 +339,7 @@ def dsn_are_equals(first, second):
_Pool = ConnectionPool(int(tools.config['db_maxconn']))
def db_connect(db_name):
- unique = db_name in ['template1', 'template0']
- return Connection(_Pool, db_name, unique)
+ return Connection(_Pool, db_name)
def close_db(db_name):
_Pool.close_all(dsn(db_name))