开发者

Should heavy-weight java objects be saved in session via setAttribute? Is it a good practice in servlet development?

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 pract开发者_JAVA百科ices here?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜