Marshal Data too short error with ActiveRecord sess. storage

I’m seeing a “marshal data too short” error with an ActiveRecord store
for my session data.

Other posts say that this happens when the size of the session data
exceeds the size of the “data” column in the sessions table. But my
“data” column is a TEXT field so it seems unlikely that I could have
blown it out.

Has anyone else seen “marshal data too short” with a database session
data store and if so, figured out a clever way around it?

Thanks,
Wes

The sessions.data column is a SQL Server “text” column, which should be
able to hold up to 2G (I think).

I am accessing the DB via ODBC and I’m thinking perhaps that’s the
issue?

Wes

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 F.

David,

Thanks for the comprehensive response. I am storing AR objects in
session but only temporarily before I actually want to persist them to
the database (then I nil them out in the session).

I will dig deeper into the attributes marshaling/unmarshaling you
mention.

Thanks,
Wes

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

Or, better yet, how to figure out what’s in the serialized session data?

Wes G. wrote:

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

SELECT datalength(data) FROM sessions;

Wes G. wrote in post #157243:

Wes G. 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