diff options
| author | Raphaël Valyi <rvalyi@gmail.com> | 2010-02-01 11:03:34 -0200 |
|---|---|---|
| committer | Raphaël Valyi <rvalyi@gmail.com> | 2010-02-01 11:03:34 -0200 |
| commit | 48e935e6a2bf4ae3f7bb333c7d25d8a6a37c20d1 (patch) | |
| tree | 90eedcd5eea138fe1934857b25c96ea5acebc31b /bin/sql_db.py | |
| parent | fe3562a57bd6b2379d4f4cdb48d72535432a2039 (diff) | |
| parent | f99cf9a615d8a4ebb2857440acecd4fac0ab9543 (diff) | |
[MERGE] from trunk
bzr revid: rvalyi@gmail.com-20100201130334-mgd708bztw2uxya9
Diffstat (limited to 'bin/sql_db.py')
| -rw-r--r-- | bin/sql_db.py | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/bin/sql_db.py b/bin/sql_db.py index a84547b0b7c..dc462526049 100644 --- a/bin/sql_db.py +++ b/bin/sql_db.py @@ -82,7 +82,6 @@ class Cursor(object): self.sql_into_log = {} self.sql_log = False self.sql_log_count = 0 - self.__closed = True # avoid the call of close() (by __del__) if an exception # is raised by any of the following initialisations self._pool = pool @@ -300,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): |
