aspose file tools*
The moose likes Servlets and the fly likes Recover Session resources when user goes away Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Recover Session resources when user goes away" Watch "Recover Session resources when user goes away" New topic
Author

Recover Session resources when user goes away

Hikari Shidou
Ranch Hand

Joined: Jan 22, 2013
Posts: 88
Hello again guys.

I will develop a web app that must use states, like login status, user data, former requests parameters, etc.

For that, I'm planning to implement View-to-Controller Broker, so that Controller has a Broker that offers all operations that Servlets can consume. This Broker object will also have these data in it. When a user logs in, a Session is created, this object is instantiated and set in the Session as an attribute.

My concern is about memory leak. I fear a user leaving and his session remaining, and its attributes hold RAM resources. I could decrease session expiration time, but then user would need to login too often.

Also, if it gets a lot of visits, I may use some pool pattern to managed reusable Brokers, instead of letting them be destroyed and construct new ones.

How can that be done with Servlet Sessions? How can I see a user has gone for like 10 minutes and take Broker out of his session, without destroying the session and forcing a new login? And how do I know a session expired and its Broker is available to be cleared and reused?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61646
    
  67

Has there been a demonstrable problem with memory? If not, then this smacks of premature optimization.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61646
    
  67

Hikari Shidou wrote:I could decrease session expiration time, but then user would need to login too often.

You do realize that the session timeout only applies after a period of inactivity, right? The session will never time out while a user is actively using the site.
Hikari Shidou
Ranch Hand

Joined: Jan 22, 2013
Posts: 88
Let's say 10K users will use the software during a month, with 1K logging at least once a day, with a maximum concurrent use of 150 users online.

My concern is that, for exemple, a session lives inside application for a week, with the object in it, resulting in 4K living sessions. Instead of having 4K objects in memory, I could have a pool of 200 objects.

But thinking better, these objects won't have so much data in them, and a normal session resource must use more RAM then them.

Also, if I take the object from a living session, when it comes back in use I won't have user data anymore, so he will need to be authenticated again.

Ok... let's hope this optimization not be needed at all and let Tomcat and garbage collector handle dead objects and just construct new ones.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61646
    
  67

A session timeout of a week is outrageous. Much more customary is about 30 minutes. Why on earth would you leave a session open for a week?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
Note that servers are NOT required to keep an active session in memory, a session can be serialized to disk whenever the server wants to - for example to free up memory.

Therefore all object references you want to keep in a session should be to Serializable objects - if not you get mysterious errors.

Bill
Hikari Shidou
Ranch Hand

Joined: Jan 22, 2013
Posts: 88
Cool! thanks a lot for the tip!
 
wood burning stoves
 
subject: Recover Session resources when user goes away