summaryrefslogtreecommitdiff
path: root/bin/sql_db.py
diff options
context:
space:
mode:
authorHDA (OpenERP) <hda@tinyerp.com>2010-01-12 16:18:30 +0530
committerHDA (OpenERP) <hda@tinyerp.com>2010-01-12 16:18:30 +0530
commit8a887ad4e09dfc9ae35204f0c31a15aeda0fb301 (patch)
treefe322bd35b9e3aef61751dff095e24227aa31e49 /bin/sql_db.py
parent7314cc0c9bbccb251ea85689068e11d75a553b57 (diff)
parent78574879c3f924b5e2314cd03afdbcc0da6cbe67 (diff)
Merged
bzr revid: hda@tinyerp.com-20100112104830-x35l4g08og2gacwo
Diffstat (limited to 'bin/sql_db.py')
-rw-r--r--bin/sql_db.py28
1 files changed, 21 insertions, 7 deletions
diff --git a/bin/sql_db.py b/bin/sql_db.py
index b4ce8347f43..dc462526049 100644
--- a/bin/sql_db.py
+++ b/bin/sql_db.py
@@ -299,17 +299,31 @@ class Connection(object):
self.dbname = dbname
self._pool = pool
self._unique = unique
- if unique:
- if dbname not in self.__LOCKS:
- self.__LOCKS[dbname] = threading.Lock()
- self.__LOCKS[dbname].acquire()
- def __del__(self):
+ def __enter__(self):
+ if self._unique:
+ self.lock()
+ return self
+
+ def __exit__(self, exc_type, exc_value, traceback):
if self._unique:
- close_db(self.dbname)
- self.__LOCKS[self.dbname].release()
+ 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()
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,))
+
return Cursor(self._pool, self.dbname, serialized=serialized)
def serialized_cursor(self):