6

Since I don't have in depth knowledge of spring session scope implementation. Can anyone please tell me if it is wise to use Spring Session scoped beans, where HttpSession object is very crucial. Like a web application where thousands of users access the site simultaneously.

Is spring session scoped bean saved in HttpSession object?

Or even if HttpSession object only refers to the spring session scoped bean, are we not making session object heavy?

How is it different form storing any bean directly in HttpSession object (making HttpSession object heavy point of view)?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Aniruddha Jagtap
  • 383
  • 5
  • 16

3 Answers3

2

The object is not really stored in HTTP session. It is linked with session id and actually stored inside of the Spring context. A session listener is used to clean instances once session is closed. See SessionScope JavaDoc.

Eugene Kuleshov
  • 31,461
  • 5
  • 66
  • 67
  • 1
    I'm not sure whether you are wrong or not, (and I'm so tired to jump in to Spring sources right now), but what about session replication in a cluster? It is hard for me to believe this is how the Spring works! – Amir Pashazadeh Feb 12 '12 at 21:55
  • 5
    Looking at the Spring source, this doesn't seem to be accurate. The session-scoped beans *are* actually stored inside the `HttpSession` object itself. `SessionScope` ultimately ends up calling `ServletRequestAttributes.setAttribute()`. – Eric Feb 27 '15 at 17:18
1

Here's what the Spring docs say:

Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.

"Heavy"? No heavier than the object you're putting in it. Sessions should have a finite lifetime. A few kbytes per session won't be the end of your application. A simple calculation for the number of simultaneous sessions and object memory requirements should reassure you about your app server memory settings. You can always increase min and max memory if needed.

Storing things in HTTP session happens the same whether you're a Spring bean or not. The bean factory just does some extra things to help manage the object lifecycle for you.

Eugene Kuleshov
  • 31,461
  • 5
  • 66
  • 67
duffymo
  • 305,152
  • 44
  • 369
  • 561
0

Is spring session scoped bean saved in HttpSession object?

If the HttpSession object you mentioned here is actually provided by spring-session (spring-session wraps the httpSession object in original httpRequest), the answer would be YES -> indeed, all the spring session scoped beans are saved in the httpSession provided by spring-session as attributes.

Or even if HttpSession object only refers to the spring session scoped bean, are we not making session object heavy?

No. The "heaviness" of the httpSession very much depends on the objects you put in. In addtion, session beans are meant to be ephemeral. Last but not least, the session object in micro-services world is usually offload to a stand-alone store, like Redis or HazelCast. So, it won't be considered as "heavy" in terms of memory consumption.

How is it different form storing any bean directly in HttpSession object (making HttpSession object heavy point of view)?

No difference at all (assuming you are using spring-sesion), as all the all the spring session scoped beans are saved in the httpSession provided by spring-session as attributes.

imarchuang
  • 467
  • 7
  • 17