tachyons / rails-fast-sessions

Automatically exported from code.google.com/p/rails-fast-sessions
0 stars 0 forks source link

Plugin fails with rails 2.2.2 #2

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
Installed rails 2.2.2

What is the expected output? What do you see instead?
Migrations went ok.  When application started up.. the session was added to
the database but browsing to another part of application gives this error:
  Status: 500 Internal Server Error
  Mysql::Error: MySQL server has gone away:             SELECT data
              FROM fast_sessions 
             WHERE session_id_crc = CRC32('51553e2c203ea7d54f29c7e81b87a6e4')
               AND session_id = '51553e2c203ea7d54f29c7e81b87a6e4'

What version of the product are you using? On what operating system?
Latest version.  OS = Ubuntu

Please provide any additional information below.
MySQL Server v5.0.67-0ubuntu6

Original issue reported on code.google.com by nabee...@gmail.com on 24 Nov 2008 at 10:08

GoogleCodeExporter commented 9 years ago
Experiencing exactly the same problem here, but only in dev mode (I think). I'm 
running OS X.

Original comment by brian.ro...@gmail.com on 24 Nov 2008 at 10:46

GoogleCodeExporter commented 9 years ago
Exactly me too, in dev mode, I'm running OS X.

Original comment by fly...@googlemail.com on 26 Nov 2008 at 9:47

GoogleCodeExporter commented 9 years ago
Perhaps this will work:

http://code.google.com/p/phusion-passenger/issues/detail?id=141

Original comment by primd...@gmail.com on 30 Nov 2008 at 2:16

GoogleCodeExporter commented 9 years ago
Get the same problem on windows with postresql:
PGError: not connected:             SELECT data
              FROM fast_sessions 
             WHERE session_id_md5 = md5(E'274dac20fba854e0013bf53fad5490b4')
               AND session_id = E'274dac20fba854e0013bf53fad5490b4'

Looks likes its losing its connection to the db.  The fusion link didn't fix it.

Original comment by andr...@gmail.com on 3 Dec 2008 at 8:55

GoogleCodeExporter commented 9 years ago
A quick fix is to replace all @@connection objects with a  
ActiveRecord::Base.connection_pool.with_connection wrapper around the block 
where 
@@connection is used.  So, this:

@@connection.execute <<-end_sql
              select 
create_update_session(#{@@connection.quote(session_id)},#{@@connection.quote(mar
shale
d_data)})
            end_sql

becomes:

ActiveRecord::Base.connection_pool.with_connection do |connection|
            connection.execute <<-end_sql
              select 
create_update_session(#{connection.quote(session_id)},#{connection.quote(marshal
ed_da
ta)})
            end_sql
          end

Original comment by andr...@gmail.com on 3 Dec 2008 at 10:34

GoogleCodeExporter commented 9 years ago
here is a patch for andreww's fix

@@ -2,10 +2,6 @@
   class Session
     class ActiveRecordStore
       class FastSessions
-        # Use the ActiveRecord::Base.connection by default.
-        cattr_accessor :connection
-        @@connection = ActiveRecord::Base.connection
-
         # The table name defaults to 'fast_sessions'.
         cattr_accessor :table_name
         @@table_name = 'fast_sessions'
@@ -32,23 +28,24 @@
         # If record has not been found, we'll create a fake session with empty data
         # to prevent AR from creation of a new session record.
         def self.find_by_session_id(session_id)
-          rec = @@connection.select_one <<-end_sql, 'Load Session'
-            SELECT data
-              FROM #{@@table_name} 
-             WHERE session_id_crc = CRC32(#{@@connection.quote(session_id)})
-               AND session_id = #{@@connection.quote(session_id)}
-          end_sql
-          
-          if !rec && @@fallback_to_old_table
-            rec = @@connection.select_one <<-end_sql, 'Load Session (old)'
+          ActiveRecord::Base.connection_pool.with_connection do |connection|
+            rec = connection.select_one <<-end_sql, 'Load Session'
               SELECT data
-                FROM #{@@old_table_name} 
-               WHERE session_id = #{@@connection.quote(session_id)}
+                FROM #{@@table_name} 
+               WHERE session_id_crc = CRC32(#{connection.quote(session_id)})
+                 AND session_id = #{connection.quote(session_id)}
             end_sql
-          end

-          session_data = rec ? rec['data'] : nil
-          new(:session_id => session_id, :marshaled_data => session_data)
+            if !rec && @@fallback_to_old_table
+              rec = connection.select_one <<-end_sql, 'Load Session (old)'
+                SELECT data
+                  FROM #{@@old_table_name} 
+                 WHERE session_id = #{connection.quote(session_id)}
+              end_sql
+            end
+            session_data = rec ? rec['data'] : nil
+            new(:session_id => session_id, :marshaled_data => session_data)
+          end
         end

         # Marshaling functions
@@ -67,30 +64,36 @@
           end

           # Creating table
-          @@connection.execute <<-end_sql
-            CREATE TABLE #{table_name} (
-              #{autoinc_id_field}
-              session_id_crc INT(10) UNSIGNED NOT NULL,
-              session_id VARCHAR(32) NOT NULL,
-              updated_at TIMESTAMP NOT NULL,
-              data TEXT,
-              #{autoinc_primary_key}
-              UNIQUE KEY `session_id` (session_id_crc, session_id),
-              KEY `updated_at` (`updated_at`)
-            ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-          end_sql
+          ActiveRecord::Base.connection_pool.with_connection do |connection|
+            connection.execute <<-end_sql
+              CREATE TABLE #{table_name} (
+                #{autoinc_id_field}
+                session_id_crc INT(10) UNSIGNED NOT NULL,
+                session_id VARCHAR(32) NOT NULL,
+                updated_at TIMESTAMP NOT NULL,
+                data TEXT,
+                #{autoinc_primary_key}
+                UNIQUE KEY `session_id` (session_id_crc, session_id),
+                KEY `updated_at` (`updated_at`)
+              ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
+            end_sql
+          end
         end

         # Drop session storage table
         def self.drop_table!
-          @@connection.execute "DROP TABLE #{table_name}"
+          ActiveRecord::Base.connection_pool.with_connection do |connection|
+            connection.execute "DROP TABLE #{table_name}"
+          end
         end

         # Delete old sessions from the storage table
         # _seconds_ parameter specifies how long you want to store your sessions.
         # By default, sessions stored for 1 week
         def self.delete_old!(seconds = 604800)
-          @@connection.execute "DELETE FROM #{table_name} WHERE updated_at <
UNIX_TIMESTAMP(NOW()) - #{seconds}"
+          ActiveRecord::Base.connection_pool.with_connection do |connection|
+            connection.execute "DELETE FROM #{table_name} WHERE updated_at <
UNIX_TIMESTAMP(NOW()) - #{seconds}"
+          end
         end

         #-----------------------------------------------------------------------
@@ -117,25 +120,29 @@
           marshaled_data = self.class.marshal(data)

           # Save data to DB
-          @@connection.update <<-end_sql, 'Create/Update session'
-            INSERT INTO #{@@table_name} SET
-              data = #{@@connection.quote(marshaled_data)}, 
-              updated_at = NOW(),
-              session_id_crc = CRC32(#{@@connection.quote(session_id)}),
-              session_id = #{@@connection.quote(session_id)}
-            ON DUPLICATE KEY UPDATE
-              data = #{@@connection.quote(marshaled_data)}, 
-              updated_at = NOW()
-          end_sql
+          ActiveRecord::Base.connection_pool.with_connection do |connection|
+            connection.update <<-end_sql, 'Create/Update session'
+              INSERT INTO #{@@table_name} SET
+                data = #{connection.quote(marshaled_data)}, 
+                updated_at = NOW(),
+                session_id_crc = CRC32(#{connection.quote(session_id)}),
+                session_id = #{connection.quote(session_id)}
+              ON DUPLICATE KEY UPDATE
+                data = #{connection.quote(marshaled_data)}, 
+                updated_at = NOW()
+            end_sql
+          end
         end

         # Destroy current session record
         def destroy
-          @@connection.delete <<-end_sql, 'Destroy session'
-            DELETE FROM #{@@table_name}
-             WHERE session_id_crc = CRC32(#{@@connection.quote(session_id)})
-               AND session_id = #{@@connection.quote(session_id)}
-          end_sql
+          ActiveRecord::Base.connection_pool.with_connection do |connection|
+            connection.delete <<-end_sql, 'Destroy session'
+              DELETE FROM #{@@table_name}
+               WHERE session_id_crc = CRC32(#{connection.quote(session_id)})
+                 AND session_id = #{connection.quote(session_id)}
+            end_sql
+          end
         end

       private

Original comment by google%b...@gtempaccount.com on 9 Jan 2009 at 2:04

GoogleCodeExporter commented 9 years ago
You should be able to replace @@connection with ActiveRecord::Base.connection.

It's checked out lazily and the post-dispatch hook will check the connection 
back in.

Original comment by koziar...@gmail.com on 21 Jan 2009 at 2:58