Update of /cvsroot/sqlobject/SQLObject/SQLObject
In directory sc8-pr-cvs1:/tmp/cvs-serv29472/SQLObject
Modified Files:
Cache.py DBConnection.py SQLObject.py
Log Message:
Added try:finally: around some locks, so exceptions won't cause
frozen locks.
Index: Cache.py
===================================================================
RCS file: /cvsroot/sqlobject/SQLObject/SQLObject/Cache.py,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** Cache.py 7 Sep 2003 07:17:49 -0000 1.9
--- Cache.py 26 Sep 2003 20:05:14 -0000 1.10
***************
*** 120,147 ****
def cull(self):
self.lock.acquire()
! keys = self.cache.keys()
! for i in xrange(self.cullOffset, len(keys), self.cullFraction):
! id = keys[i]
! self.expiredCache[id] = ref(self.cache[id])
! del self.cache[id]
! # This offset tries to balance out which objects we expire, so
! # no object will just hang out in the cache forever.
! self.cullOffset = (self.culldOffset + 1) % self.cullFraction
! self.lock.release()
def expire(self, id):
self.lock.acquire()
! if self.cache.has_key(id):
! del self.cache[id]
! if self.expiredCache.has_key(id):
! del self.expiredCache[id]
! self.lock.release()
def expireAll(self):
self.lock.acquire()
! for key, value in self.cache.items():
! self.expiredCache[key] = ref(obj)
! self.cache = {}
! self.lock.release()
def allIDs(self):
--- 120,154 ----
def cull(self):
self.lock.acquire()
! try:
! keys = self.cache.keys()
! for i in xrange(self.cullOffset, len(keys), self.cullFraction):
! id = keys[i]
! self.expiredCache[id] = ref(self.cache[id])
! del self.cache[id]
! # This offset tries to balance out which objects we
! # expire, so no object will just hang out in the cache
! # forever.
! self.cullOffset = (self.culldOffset + 1) % self.cullFraction
! finally:
! self.lock.release()
def expire(self, id):
self.lock.acquire()
! try:
! if self.cache.has_key(id):
! del self.cache[id]
! if self.expiredCache.has_key(id):
! del self.expiredCache[id]
! finally:
! self.lock.release()
def expireAll(self):
self.lock.acquire()
! try:
! for key, value in self.cache.items():
! self.expiredCache[key] = ref(obj)
! self.cache = {}
! finally:
! self.lock.release()
def allIDs(self):
Index: DBConnection.py
===================================================================
RCS file: /cvsroot/sqlobject/SQLObject/SQLObject/DBConnection.py,v
retrieving revision 1.49
retrieving revision 1.50
diff -C2 -d -r1.49 -r1.50
*** DBConnection.py 26 Sep 2003 07:12:59 -0000 1.49
--- DBConnection.py 26 Sep 2003 20:05:14 -0000 1.50
***************
*** 73,84 ****
def getConnection(self):
self._poolLock.acquire()
! if not self._pool:
! newConn = self.makeConnection()
! self._pool.append(newConn)
! self._connectionNumbers[id(newConn)] = self._connectionCount
! self._connectionCount += 1
! val = self._pool.pop()
! self._poolLock.release()
! return val
def releaseConnection(self, conn):
--- 73,86 ----
def getConnection(self):
self._poolLock.acquire()
! try:
! if not self._pool:
! newConn = self.makeConnection()
! self._pool.append(newConn)
! self._connectionNumbers[id(newConn)] = self._connectionCount
! self._connectionCount += 1
! val = self._pool.pop()
! return val
! finally:
! self._poolLock.release()
def releaseConnection(self, conn):
Index: SQLObject.py
===================================================================
RCS file: /cvsroot/sqlobject/SQLObject/SQLObject/SQLObject.py,v
retrieving revision 1.59
retrieving revision 1.60
diff -C2 -d -r1.59 -r1.60
*** SQLObject.py 26 Sep 2003 07:12:59 -0000 1.59
--- SQLObject.py 26 Sep 2003 20:05:14 -0000 1.60
***************
*** 674,692 ****
return getattr(self, attrName)
except AttributeError:
- self._SO_writeLock.acquire()
try:
! # Maybe, just in the moment since we got the lock,
! # some other thread did a _SO_loadValue and we have
! # the attribute! Let's try and find out! We can keep
! # trying this all day and still beat the performance
! # on the database call (okay, we can keep trying this
! # for a few msecs at least)...
result = getattr(self, attrName)
- except AttributeError:
- pass
- else:
- self._SO_writeLock.release()
return result
! self._expired = False
dbNames = [col.dbName for col in self._SO_columns]
selectResults = self._connection._SO_selectOne(self, dbNames)
--- 674,705 ----
return getattr(self, attrName)
except AttributeError:
try:
! self._SO_writeLock.acquire()
! try:
! # Maybe, just in the moment since we got the lock,
! # some other thread did a _SO_loadValue and we
! # have the attribute! Let's try and find out! We
! # can keep trying this all day and still beat the
! # performance on the database call (okay, we can
! # keep trying this for a few msecs at least)...
! result = getattr(self, attrName)
! except AttributeError:
! pass
! else:
! return result
! self._expired = False
! dbNames = [col.dbName for col in self._SO_columns]
! selectResults = self._connection._SO_selectOne(self, dbNames)
! if not selectResults:
! raise SQLObjectNotFound, "The object %s by the ID %s has been deleted" % (self.__class__.__name__, self.id)
! self._SO_selectInit(selectResults)
result = getattr(self, attrName)
return result
! finally:
! self._SO_writeLock.release()
!
! def sync(self):
! self._SO_writeLock.acquire()
! try:
dbNames = [col.dbName for col in self._SO_columns]
selectResults = self._connection._SO_selectOne(self, dbNames)
***************
*** 694,710 ****
raise SQLObjectNotFound, "The object %s by the ID %s has been deleted" % (self.__class__.__name__, self.id)
self._SO_selectInit(selectResults)
! result = getattr(self, attrName)
self._SO_writeLock.release()
- return result
-
- def sync(self):
- self._SO_writeLock.acquire()
- dbNames = [col.dbName for col in self._SO_columns]
- selectResults = self._connection._SO_selectOne(self, dbNames)
- if not selectResults:
- raise SQLObjectNotFound, "The object %s by the ID %s has been deleted" % (self.__class__.__name__, self.id)
- self._SO_selectInit(selectResults)
- self._expired = False
- self._SO_writeLock.release()
def expire(self):
--- 707,713 ----
raise SQLObjectNotFound, "The object %s by the ID %s has been deleted" % (self.__class__.__name__, self.id)
self._SO_selectInit(selectResults)
! self._expired = False
! finally:
self._SO_writeLock.release()
def expire(self):
***************
*** 712,723 ****
return
self._SO_writeLock.acquire()
! if self._expired:
self._SO_writeLock.release()
- return
- for column in self._SO_columns:
- delattr(self, instanceName(column.name))
- self._expired = True
- self._connection.cache.expire(self.id, self.__class__)
- self._SO_writeLock.release()
def _SO_setValue(self, name, value, fromPython):
--- 715,727 ----
return
self._SO_writeLock.acquire()
! try:
! if self._expired:
! return
! for column in self._SO_columns:
! delattr(self, instanceName(column.name))
! self._expired = True
! self._connection.cache.expire(self.id, self.__class__)
! finally:
self._SO_writeLock.release()
def _SO_setValue(self, name, value, fromPython):
***************
*** 757,783 ****
self._SO_writeLock.acquire()
! # We have to go through and see if the setters are
! # "plain", that is, if the user has changed their
! # definition in any way (put in something that
! # normalizes the value or checks for consistency,
! # for instance). If so then we have to use plain
! # old setattr() to change the value, since we can't
! # read the user's mind. We'll combine everything
! # else into a single UPDATE, if necessary.
! toUpdate = {}
! for name, value in kw.items():
! if self._SO_plainSetters.has_key(name):
! fromPython = getattr(self, '_SO_fromPython_%s' % name)
! if fromPython:
! value = fromPython(value, self._SO_validatorState)
! toUpdate[name] = value
! if self._cacheValues:
! setattr(self, instanceName(name), value)
! else:
! setattr(self, name, value)
! if toUpdate:
! self._connection._SO_update(self, [(self._SO_columnDict[name].dbName, value) for name, value in toUpdate.items()])
! self._SO_writeLock.release()
def _SO_selectInit(self, row):
--- 761,789 ----
self._SO_writeLock.acquire()
! try:
! # We have to go through and see if the setters are
! # "plain", that is, if the user has changed their
! # definition in any way (put in something that
! # normalizes the value or checks for consistency,
! # for instance). If so then we have to use plain
! # old setattr() to change the value, since we can't
! # read the user's mind. We'll combine everything
! # else into a single UPDATE, if necessary.
! toUpdate = {}
! for name, value in kw.items():
! if self._SO_plainSetters.has_key(name):
! fromPython = getattr(self, '_SO_fromPython_%s' % name)
! if fromPython:
! value = fromPython(value, self._SO_validatorState)
! toUpdate[name] = value
! if self._cacheValues:
! setattr(self, instanceName(name), value)
! else:
! setattr(self, name, value)
! if toUpdate:
! self._connection._SO_update(self, [(self._SO_columnDict[name].dbName, value) for name, value in toUpdate.items()])
! finally:
! self._SO_writeLock.release()
def _SO_selectInit(self, row):
***************
*** 920,925 ****
if not obj._cacheValues:
obj._SO_writeLock.acquire()
! obj._SO_selectInit(result[1:])
! obj._SO_writeLock.release()
return obj
_SO_fetchAlternateID = classmethod(_SO_fetchAlternateID)
--- 926,933 ----
if not obj._cacheValues:
obj._SO_writeLock.acquire()
! try:
! obj._SO_selectInit(result[1:])
! finally:
! obj._SO_writeLock.release()
return obj
_SO_fetchAlternateID = classmethod(_SO_fetchAlternateID)
|