Marshal Data too short error with ActiveRecord sess. storage

I've run into this before, not with sessions, but with some other data stored in a table.

The issue, from memory, was something to do with this line in my.cnf:

[mysqld] max_allowed_packet = 1M

Which basically means (IIRC) that the longest query you may send to the server is 1MB. This means that if your marhalled session data is bigger than that (plus the overhead of the statement) then you will get a "Packet too large" error and AR's connection will be severed.

I can't remeber if this is the exact reason the problem occurred, but upping the value did fix it.

However, if you're storing more than a megabyte in session then you probably have bigger problems than worrying about truncated session data - namely the huge overhead of continuously reading and updating this data with EVERY SINGLE REQUEST. Once you get up around a large number of requests, that's a lot of data being piped around, which probably ain't a good thing.

I'd guess you're probably storing AR objects in session, which is fine, but make sure that their associated objects don't get marshalled as well, otherwise you might end up with a massive association tree being stored as well. Either just store the ID of the object in question, or modify your AR classes so the associated classes aren't marshalled - I did this by adding the following mixin to my models to make sure only the "attributes" hash was marshalled for the ones I stored in session:

module OnlyMarshalAttributes   def marshal_dump     @attributes   end

  def marshal_load(data)     @attributes = data   end end

class MyModel < ActiveRecord::Base   # Make sure only local data gets marshalled   include OnlyMarshalAttributes

  has_and_belongs_to :other_models   has_many :different_things end

There may be further caveats to marshalling/unmarshalling only @attributes (and accessing it directly is bad practice, but hey, it gets the job done in this case) so be aware that it may not be fully future proof. For now though, it seems to work pretty well.

Hope that helps!

-David Felstead

Wes Gamble wrote in post #157243:

Wes Gamble wrote:

Does anyone know how to figure out the size of a text column in SQL Server?

SELECT datalength(data) FROM sessions;

Hello my friend. I have the same sitiutaion at now. Do you find a solution for that?

regards Coban