views:

86

answers:

2

I mean if I construct a heavy object [including collections and a bunch of props ] and I need to query that object from time to time during the session life, should I save it with setAttribute or do I need to persist it somewhere? What are best practices here?

+1  A: 

If you want it cached, place it in an external cache (2nd level cache, memcached or another cache server), not in the session. The session should be kept as small as possible, as the server may serialise it to disk.

David Rabinowitz
A: 

Let's assume your heavy object can be rebuilt from your persistence layer whenever you need to, and that this is purely an optimization. Then this really turns into an "it depends". If your web application runs on a single server and doesn't get much use, it doesn't much matter either way, so pick whatever solution seems simplest to you.

If you're running the web application on a whole cluster of application servers, with lots of usage, you generally want to avoid session state for scalability. But you can still cache these structures outside the app server cluster for performance (eg, in an HTTP cache or a memcached distributed cache).

Then there's a broad middle ground where the app server cluster can be run with sticky sessions (traffic for each session gets routed to the same app server) or with cluster support for sessions (it maintains the session data, and migrates it to whatever server needs it).

Jim Ferrans