def persist_db(self): """ Persist the session state to the database. This method will call _gc() to garbage collect the session specific database connection if it exists. """ # Keep track of what happens waspersisted = False # Indicate a successfull db persist [rollback if necessary] current_stale_count = self[stale_count] self[stale_count] = 0 # Prepare the sql sql = "SELECT session_set(%s, %s, TRUE);" sql = sql % (db.cstr(self._guid), db.cstr(json.encode(self))) # Attempt the persist try: self.connect_db() self._cursor.execute(sql) self._conn.commit() waspersisted = True except: try: self._conn.rollback() except: pass self.flush_next_persist() self[stale_count] = current_stale_count finally: # Because persistance is always at the end of the process # flow (actually called by apache.handler) we can safely # close the database connection now, this is the last db # work to be done. self._gc() # If the db persist failed for whatever reason, try to # failover on the cache till it comes back up. if not waspersisted: if self.persist_cache(): waspersisted = True # Raise if we can't persist if not waspersisted: raise error.SessionUnableToPersistError()
def fetch_from_db(self): """ Fetch a user's session from the database. @return: Native Python object, or None if none found """ sql = \ """ SELECT values FROM session WHERE guid = %s AND active = TRUE; """ % (db.cstr(self._guid)) try: self.connect_db() self._cursor.execute(sql) self._record = self._cursor.fetchone() except: #self._conn.error.OperationalError, ex: return {'SESSION-ERROR':'DATABASE UNAVAILABLE!'} if self._record is None: return {} else: try: return json.decode(self._record['values']) except ValueError, ex: raise "Unable to json.decode session", ex
def persist(self, encoded): self.log.debug('persist() called') sql = "SELECT session_set(%s, %s, TRUE);" sql = sql % (db.cstr(self.guid), db.cstr(encoded)) # Attempt the persist try: self.cursor.execute(sql) self.conn.commit() self.log.debug('Persisted: OK') return True except Exception, ex: try: self.conn.rollback() except: pass
def destroy(self): sql = "DELETE FROM SESSION WHERE guid = %s;" % db.cstr(self.guid) try: self.cursor.execute(sql) self.conn.commit() return True except: self.conn.rollback() raise return False
def fetch_session(self): self.log.debug('fetching data from postgresql') sql = "SELECT values FROM session WHERE guid = %s AND active = TRUE;" sql = sql % db.cstr(self.guid) row = None try: self.cursor.execute(sql) row = self.cursor.fetchone() except Exception, ex: #self.conn.error.OperationalError, ex: self.log.warning('guid: %s', self.guid, exc_info=True, extra=EXTRA) return None
def destroy(self): """ Expire a user's session now. This does persist to the database and cache immediately. """ sql = "DELETE FROM SESSION WHERE guid = %s;" % db.cstr(self._guid) try: self.connect_db() self._cursor.execute(sql) self._conn.commit() self.isauthenticated = False except: self._conn.rollback() raise finally: self._gc() # Delete from cache if not self._cache is None: self._cache.delete(self.mkey()) # Ensure the data still in memory (self) is not persisted back self._expired = True