views:

221

answers:

1

The reason I ask this is recentlydesigned an application , initially wanted to use no session state to keep things simple, but eventually succumbed to the ease of the session state idea for storing state. I was aware that moving to a web farm solution would involve configuring session state to run in sql. However I had never run across any information explaining serialization would be neccessary. So no I need to refactor code to achieve this.

Additionally, I use linq to sql to give me data objects. Ideally this would be easy to serialise, but it seems not straight forward.

I have googled and doing this should be ok. The question isn't how to serialise the linq data class but rather:

Is session state the way forward, in particular when designing scalable applications on a web farm, should we use other methods, what's recommended when using linq for data access, should we use query string etc.

Thanks

+1  A: 

There are tons (really) ways of doing this

Storing session state is a basic requirement for many web-farms/apps. You can use LINQ->SQL to store it in a database or you could use flat-files on a SAN. It really doesn't matter (other than form a performance point of view) providing you have some method to serialize from storage format to native language format.

In PHP the Session handler maps to a session table as strings. In Python you might marshall an object to a string and store that.

I would recommend an SQL backend accessed in any form, because RDBMS are designed for concurrent access so you don't need to worry about locking and so on. And it scales as well as the RDBMS

You could use something like SQL Alchemy

Session = sqlalchemy.orm.sessionmaker() session = Session()

From http://www.darrellhawley.com/2009/01/sql-alchemy-orm-basics.html

Or here

http://idunno.org/articles/277.aspx

Or this HOWTO from Microsoft called "HOW TO: Configure SQL Server to Store ASP.NET Session State" might help

http://support.microsoft.com/kb/317604

Which provides

<sessionState 
    mode="SQLServer"
    sqlConnectionString="data source=127.0.0.1;user id=<username>;password=<strongpassword>"
    cookieless="false" 
        timeout="20" 
/>

Without looking at your apps needs, then determining if sessions state is required is impossible. But at the end of the day, state is state, and the requirements of access to that state (read-heavy, write heavy, big, small, whatever) determine how you store that and in what format.

I would recommend either an ORM mapping between a hashmap-like object to an SQL table or serialization of a hashmap such that

session = load_from_sql(session_id)
someval = session['var']

Keeping values as strings or native objects depends on the ORM you select, how easy it is from your language and so on. Performance is also a factor, as well as database footprint. You may find it simpler to store strings of simple variables and unmarshall-on-demand. You could store JSON types and then convert them to native.

Alot to consider...

SQL is probably simplest, be it ORM->session_object or a bunch of tuples mapped to a hashtable.

Good Luck

Aiden Bell
thanks for the answer, setting up session state I'm ok with, I was just convinced there must be an easier way of dealing with serialisation. The integers and strings need to be structs or classes I think? or the catch all approach and serialise the linq data classes?
Stuart
Depends on how complex the data is you want to store. If it just strings (which can be converted to numbers) then a session_tbl with key,value will do OK. Then it is just a session object with .get(key) and .set(key) after init on session_id. If you are storing more complex data, use an ORM. But an SQL backend is the way to go I recon :) Hope that helps.
Aiden Bell