Commits

Kris Hardy  committed 12ca671

Fixing bugs in PostgreSQL session handling.

  • Participants
  • Parent commits 701f413

Comments (0)

Files changed (1)

File cherrypy/lib/sessions.py

     pickle_protocol = pickle.HIGHEST_PROTOCOL
     
     def __init__(self, id=None, **kwargs):
+        self.cursor = self.db.cursor()
         Session.__init__(self, id, **kwargs)
-        self.cursor = self.db.cursor()
     
     def setup(cls, **kwargs):
         """Set up the storage system for Postgres-based sessions.
         for k, v in kwargs.items():
             setattr(cls, k, v)
         
-        self.db = self.get_db()
+        cls.db = cls.get_db()
     setup = classmethod(setup)
     
     def __del__(self):
-        if self.cursor:
+        if self.cursor and self.cursor.closed is False:
             self.cursor.close()
         self.db.commit()
     
     def _exists(self):
         # Select session data from table
-        self.cursor.execute('select data, expiration_time from session '
-                            'where id=%s', (self.id,))
-        rows = self.cursor.fetchall()
-        return bool(rows)
+        self.cursor.execute('select 1 from session '
+                            'where id=%s limit 1', (self.id,))
+        count = self.cursor.fetchone()
+        return count is not None
     
     def _load(self):
         # Select session data from table
         """Release the lock on the currently-loaded session data."""
         # We just close the cursor and that will remove the lock
         #   introduced by the "for update" clause
-        self.cursor.close()
+        if self.cursor.closed is False:
+            self.cursor.close()
         self.locked = False
     
     def clean_up(self):